简体   繁体   English

在Facebook张贴在好友墙上时,Android中的Oauth错误消息“缺少消息或附件”,“类型”:“ OAuthException”,“代码”:100

[英]Oauth error message “Missing message or attachment”, “type”:“OAuthException”,“code”:100 in android while Facebook post on friends wall

I want to give user of my application an option to log-in into my application using Facebook account. 我想给我的应用程序用户一个使用Facebook帐户登录到我的应用程序的选项。 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. 现在,我正尝试邀请所有Facebook朋友加入我的android应用程序。 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: 为此,我正在尝试通过此链接编写代码, 向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");

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.putByteArray for text fields? 为什么将params.putByteArray用于文本字段? I understand the "picture" field. 我了解“图片”字段。 But a simple params.putString will suffice the "message", "link", "caption", "description" and "name" fields. 但是,一个简单的params.putString满足“消息”,“链接”,“标题”,“描述”和“名称”字段。

Also, you are passing the "message" parameter twice. 另外,您要两次传递“ message”参数。 Once here: params.putString("message", "msg"); 曾经在这里: params.putString("message", "msg");

and another here: params.putByteArray("message", "message goes here".getBytes()); 另一个在这里: params.putByteArray("message", "message goes here".getBytes());

You code should essentially look like this: 您的代码本质上应如下所示:

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());

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()); 话虽这么说,我对此不太确定: params.putByteArray("picture", "http://url.to.my.picture/pic.jpg".getBytes());

I only upload photos from either the Gallery or the Camera. 我只上传来自图库或相机的照片。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM