我想为我的应用程序的用户提供使用Facebook帐户登录我的应用程序的选项。我可以使用下面的代码来做到这一点。
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();
}
}
}
}
现在我正试图邀请我所有的Facebook好友加入我的Android应用程序。为此,我正在尝试来自此链接的代码send an invitation to facebook friends to join my website,以及它的修改代码(因为它不起作用),现在代码是:
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");
但它给出了错误
12-03 19:46:04.552: D/Tests(873): {"error":{"message":"(#100) Missing message or attachment","type":"OAuthException","code":100}}
所以请帮我删除此错误。
答案 0 :(得分:0)
为什么使用params.putByteArray
作为文本字段?我理解“图片”字段。但是简单的params.putString
就足以满足“消息”,“链接”,“标题”,“描述”和“名称”字段的需要。
此外,您正在传递“message”参数两次。
到达此处后:params.putString("message", "msg");
和另一个:params.putByteArray("message", "message goes here".getBytes());
您的代码应该基本上如下所示:
Bundle params = new Bundle();
params.putString("message", "msg");
params.putString(mFacebook.getAccessToken(), "msg");
params.putString("message", "message goes here".getBytes()); // CHOOSE FROM EITHER THE ONE ON THIS LINE OR THIS ONE: params.putString("message", "msg");
params.putString("link", "http://mysite.com".getBytes());
params.putString("caption", "Click the link".getBytes());
params.putString("description", "description of link".getBytes());
params.putString("name", "name of link".getBytes());
params.putByteArray("picture", "http://url.to.my.picture/pic.jpg".getBytes());
我为我的应用程序使用完全相同的代码(好吧,几乎无论如何),它工作得很好。话虽如此,我对这一点不太确定:params.putByteArray("picture", "http://url.to.my.picture/pic.jpg".getBytes());
我只上传图库或相机中的照片。