2

我想为我的应用程序用户提供使用 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 应用程序。为此,我正在尝试来自此链接的代码,向 facebook 朋友发送加入我的网站的邀请,并修改它的代码(因为它不起作用),现在代码是:

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}}

所以请帮我消除这个错误。

4

1 回答 1

0

为什么要使用params.putByteArray文本字段?我了解“图片”字段。但是一个简单params.putString的“消息”、“链接”、“标题”、“描述”和“名称”字段就足够了。

此外,您将两次传递“消息”参数。一旦来到这里: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());

我只从画廊或相机上传照片。

于 2012-12-03T15:07:29.500 回答