I want to give user of my application an option to log-in into my application using Facebook account. And I'm able to do that using below code.
String[] permissions = { "offline_access", "publish_stream", "user_photos", "publish_checkins", "photo_upload" };
mFacebook.authorize(FacebookLogin.this, permissions, new LoginDialogListener());
public final class LoginDialogListener implements DialogListener {
public void onComplete(Bundle values) {
new MyAsyncGetJsonFromFacebbok(FacebookLogin.this).execute();
}
public void onFacebookError(FacebookError error) {
Toast.makeText(FacebookLogin.this, "Something went wrong. Please try again.", Toast.LENGTH_LONG).show();
Log.v("onFacebookError FacebookLogin", error.toString());
}
public void onError(DialogError error) {
Toast.makeText(FacebookLogin.this, "Something went wrong. Please try again.", Toast.LENGTH_LONG).show();
Log.v("onError FacebookLogin", error.toString());
}
public void onCancel() {
Toast.makeText(FacebookLogin.this, "Something went wrong. Please try again.", Toast.LENGTH_LONG).show();
}
public class MyAsyncGetJsonFromFacebbok extends AsyncTask<Void, Void, JSONObject> {
Context context;
JSONObject jsonObj = null;
public MyAsyncGetJsonFromFacebbok(Context context_) {
this.context = context_;
}
@Override
protected JSONObject doInBackground(Void... params) {
try {
jsonObj = com.facebook.android.Util.parseJson(mFacebook.request("me"));
} catch (FacebookError e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return jsonObj;
}
@Override
protected void onPostExecute(JSONObject jsonObj) {
try {
String facebookID = jsonObj.getString("id");
String firstName = jsonObj.getString("first_name");
String lastName = jsonObj.getString("last_name");
Toast.makeText(FacebookLogin.this, "Thank you for Logging In, " + firstName + " " + lastName + "!", Toast.LENGTH_SHORT)
.show();
SessionStore.save(mFacebook, FacebookLogin.this);
storeDataInSharedPreferrence(facebookID, firstName, lastName);
sendInvitationToFriends(facebookID);
startNextActivity();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Now I'm trying to give invitation to all my Facebook friends to join my android application. For that I'm trying a code from this link, send an invitation to facebook friends to join my website, and modified code of it(because it wasn't working), now the code is:
String response = mFacebook.request((userID == null) ? "me" : userID);
Bundle params = new Bundle();
params.putString("message", "msg");
params.putString(mFacebook.getAccessToken(), "msg");
params.putByteArray("message", "message goes here".getBytes());
params.putByteArray("link", "http://mysite.com".getBytes());
params.putByteArray("caption", "Click the link".getBytes());
params.putByteArray("description", "description of link".getBytes());
params.putByteArray("name", "name of link".getBytes());
params.putByteArray("picture", "http://url.to.my.picture/pic.jpg".getBytes());
response = mFacebook.request(((userID == null) ? "me" : userID) + "/feed", params, "POST");
but its giving the error
12-03 19:46:04.552: D/Tests(873): {"error":{"message":"(#100) Missing message or attachment","type":"OAuthException","code":100}}
So please help me to remove this error.
Why are you using
params.putByteArrayfor text fields? I understand the "picture" field. But a simpleparams.putStringwill suffice the "message", "link", "caption", "description" and "name" fields.Also, you are passing the "message" parameter twice. Once here:
params.putString("message", "msg");and another here:
params.putByteArray("message", "message goes here".getBytes());You code should essentially look like this:
I use the exact same code (well, almost anyway) for my app and it works just fine. That being said, I am not so sure about this one:
params.putByteArray("picture", "http://url.to.my.picture/pic.jpg".getBytes());I only upload photos from either the Gallery or the Camera.