Do intents no longer work for posting images on Facebook? - java

I created an app some time back, using intents. Now it is not working. I learned somewhere that text and image posting through intents are not allowed. I just want to confirm this is the case.
If this is true, then is the only method to post via the Facebook SDK or are there any other simple ways to do it?

What you can do is utilize the facebook graph api to do HTTP requests on behalf of the user to the facebook server using the access token.
In order to get the access token you need to implement the facebook login, you need to be familiar with oauth and oauth2 protocols for doing this.
Once you have the access token you can post the image to the facebook account at the /me/photosendpoint.
Store you image in a Bitmap. And use the following code.
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(
"https://graph.facebook.com/me/photos?access_token="
+ AccessTokens.fbaccesstoken);
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
entity.addPart("source", new ByteArrayBody(data, "myImage.jpg"));
entity.addPart("message", new StringBody(caption.getText()
.toString()));
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost,localContext);
The facebook sdk is built on top of this API, the sdk considerably reduces the complexity in using these features for us developers.
P.S: Facebook has a review for any apps trying to implement oauth login. More info at https://developers.facebook.com/docs/facebook-login/review

Related

Sharepoint REST Api in Java: Unable to access site documents

I am trying to access sharepoint site documents using SP REST API in java. I am able to authenticate and get a response for the below URL
http://sharepoint_server_url/_api/lists/getbytitle('Documents')
But I want to get the list of folders and files inside my site. I tried the following URL, but it gives me 401 Unauthorized.
http://sharepoint_server_url/sites/mysitename/_api/lists/getbytitle('Documents')
I am using the NTCredentials class, to authenticate.
Please let me know if
I have to make some setting in the Sharepoint server for mysite, so that I can access it through API?
Or, The URL above is wrong, I have to change it?
This is the code used for authentication :
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(AuthScope.ANY),
new NTCredentials("username", "password", "http://server_DNS", "DOMAIN"));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.build();
HttpGet httpget = new HttpGet(url);
httpclient.execute(httpget);
I am sure someone must have done this already.
Thanks in advance.
You can take a look of this project i've created for using the ShareopintRestAPI from java. I decided to make my own implementation as I've struggled a lot with other APIs i found and i was not able to make them work.
You can take a look here
https://github.com/kikovalle/PLGSharepointRestAPI-java

Uploading images to Google App Engine using a standalone Java application

I'm writing a simple, no UI (hence no .jsp/.html files), console based Java application to read image file paths from a file and upload the images to my sample photo feed application on Google App Engine.
I'm using Apache HttpClient to make the URL connection and the POST request to my app engine application and this is the code I have so far.
MultipartEntity multiPartEntity = new MultipartEntity();
multiPartEntity.addPart("photo", new FileBody(new File(filePath)));
multiPartEntity.addPart("title", new StringBody(comment));
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(uploadUrl);
post.setEntity(multiPartEntity);
HttpResponse response = client.execute(post);
This uploads the file and I can see it in the Blob Viewer. However, the Content-Type of the file is set to "application/octet-stream".
I want the Content-Type to be set to "image/jpeg" or "image/png" or so on depending on the image type. I tried modifying the code a bit using
MultipartEntity multiPartEntity = new MultipartEntity();
multiPartEntity.addPart("photo", new FileBody(new File(filePath), "image/jpeg"));
multiPartEntity.addPart("title", new StringBody(comment));
but this failed to even upload the file into the Blob Viewer and I still get the same response from the application servlet.
Can somebody help me crack this?

Android login and redirect

I am trying to get some data from a webpage that needs login in a page. I could do with htmlunit but gave me problems import these libraries on Android. So I am trying to do it with apache http client.
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("https://inside.cineca.it/cgi-bin/uinside/marcature.pl");
BasicNameValuePair usernameBasicNameValuePair = new BasicNameValuePair("j_username", "user");
BasicNameValuePair passwordBasicNameValuePAir = new BasicNameValuePair("j_password", "pass");
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
nameValuePairList.add(usernameBasicNameValuePair);
nameValuePairList.add(passwordBasicNameValuePAir);
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairList);
HttpResponse httpResponse = httpClient.execute(httpPost);
System.out.println(EntityUtils.toString(httpResponse.getEntity()));
But is giving me error. I am affraid i should manage the redirect but i don't know how. I would be happy if someone could give me some advice.
Best regards
You're performing an http POST to a login page. Maybe the server that serves you back the login page doesn't know to handle the post. Anyway, I am sure your intention is to perform the login actually. You need to figure out what is the server API where you should post the credentials and process the result. Performing a quick investigation with Chrome Network profiler, the login API for above link actually resides at: https://idp-is.cineca.it/idp/Authn/Multilogin. That is the URL that you should be using and there is where you need to POST.
Also, make sure the request you're making is done on a non-UI thread as this is a common mistake done by beginners.

Android Rest Client

I found so many samples for requesting a REST API, but all together are confusing, can some one please explain me a way to use http requests.
My Requirement is, I want to get data from a REST API by providing username, pwd and a key.
What I have Used was,
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("REST API url");
post.setHeader("Content-type", "application/json");
JSONObject obj = new JSONObject();
obj.put("username", "un");
obj.put("pwd", "password");
obj.put("key","123456");
post.setEntity(new StringEntity(obj.toString(), "UTF-8"));
HttpResponse response = client.execute(post);
But the response is always null and these working fine when tested with browser tool by posting the same data.Is some thing wrong with my approach? please suggest me the correct way. Thank you
(1) Google I/O video session for developing REST clients
(2) search in android developer blog
(3) https://github.com/darko1002001/android-rest-client
Please try after that post your question,
I can share code snippet from my rest client developed based on (1) & (2)
Do not use Cloud to Device Messaging, instead use the latest cloud approach with android application development.
There is new library called Volley, which looks better than AsyncTask. It should be useful in developing RESTful clients.
You probably forgot to add the internet permission to the manifest file.
Add the following line.
<uses-permission android:name="android.permission.INTERNET" />
I thing you should try this,
HttpContext localContext = new BasicHttpContext();
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("REST API url");
post.setHeader("Content-type", "application/json");
JSONObject obj = new JSONObject();
obj.put("username", "un");
obj.put("pwd", "password");
obj.put("key","123456");
post.setEntity(new StringEntity(obj.toString(), "UTF-8"));
HttpResponse response = client.execute(post,localContext);
Hope this will help.
By any chance, is the server expecting a GET request for this operation? If so, you may want to use HttpGet instead of HttpPost.

How to build an HttpClient interactive with remote server using Java

I need to coding an Http Client using java which interact with a stateful http server. The client needs to
navigate to login page and accept cookies
submit login page with http form field filled
select goods and add to shopping cart
submit shopping cart
I am trying to use HttpClient to implement this client. However I found even I submitted the login form, it still return the login form just like my submit is invalid. Here is my code:
HttpClient agent = new DefaultHttpClient();
agent.getParams().setParameter(ClientPNames.COOKIE_POLICY,
CookiePolicy.RFC_2965);
CookieStore cookieStore = new BasicCookieStore();
HttpContext localContext = new BasicHttpContext();
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpGet httpget = new HttpGet(site);
HttpResponse response = agent.execute(httpget, localContext);
HttpEntity entity = response.getEntity();
entity.getContent().close();
HttpPost post = new HttpPost(site + "/login.aspx");
post.getParams().setParameter("LoginControl1$ctlLoginName", "myusername");
post.getParams().setParameter("LoginControl1$ctlPassword", "mypassword");
response = agent.execute(post, localContext);
entity = response.getEntity();
String s = IO.readContentAsString(entity.getContent());
System.out.println(s);
Any idea where I am wrong? Or do you have better way to implement this?
Thanks a lot
Green
Not really sure what might be wrong, but have you considered using tcpdump/Wireshark to grab the raw HTTP conversation with the server and compare what you're sending/receiving in HTTPClient with what you send/receive in your web browser when you submit the form?

Categories