需要扩展权限publish_actions

时间:2016-05-30 04:36:56

标签: android facebook

我尝试使用最新的facebook-sdk开发登录和共享图像,我成功实现了登录,但是当我尝试分享照片虽然我检查了publish_actions,但它没有提示我允许..并且错误显示为< strong>(#200)需要扩展权限publish_actions ..更好是我的代码。请帮助我,我做错了什么......任何帮助都是可以帮助的。

package com.example.sociallogin;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.example.sociallogin.Utility.Utils;
import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.HttpMethod;
import com.facebook.Profile;
import com.facebook.appevents.AppEventsLogger;
import com.facebook.internal.Utility;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Set;

public class MainActivity extends AppCompatActivity {
    Button button, shareBtn, picPublish;
    CallbackManager callbackManager;
    AccessToken accessToken;
    String name, email, first_name, last_name, gender, bday, id, profile_pic_url;
    private ArrayList<String> picList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = new CallbackManager.Factory().create();
        picList.add("http://placehold.it/120x120&text=image1");
        picList.add("http://placehold.it/120x120&text=image2");
        picList.add("http://placehold.it/120x120&text=image3");
        picList.add("http://placehold.it/120x120&text=image4");
        //AppEventsLogger.activateApp(this);
        button = (Button) findViewById(R.id.btn_login);
        shareBtn = (Button) findViewById(R.id.btn_share);
        picPublish = (Button) findViewById(R.id.pic_publish);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginwithFb();
            }
        });


    }

    private void loginwithFb() {
        LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "email", "user_actions.fitness"));
        LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(final LoginResult loginResult) {
                Utils.showToast(MainActivity.this, "Succes");
                accessToken = loginResult.getAccessToken();

                GraphRequest graphRequest = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
                    @Override
                    public void onCompleted(JSONObject object, GraphResponse response) {
                        try {
                            String email = object.getString("email");
                            name = object.getString("name");
                            first_name = object.optString("first_name");
                            last_name = object.optString("last_name");
                            gender = object.getString("gender");
                            //bday=object.getString("birthday");
                            id = object.getString("id");
                            profile_pic_url = "https://graph.facebook.com/" + id + "/picture?type=large";
                            Utils.showToast(MainActivity.this, first_name);
                            /*Profile profile=Profile.getCurrentProfile();
                            profile.getProfilePictureUri(200,200);*/
                            Log.i("info", "Email id is" + email);
                            Log.i("info", "First Name is " + first_name);
                            Log.i("info", "Last Name is " + last_name);
                            Log.i("info", "Gender is" + gender);
                            //Log.i("info","Birth Date is"+ bday);
                            Log.i("info", "Id is " + id);
                            Log.i("info", "Url is " + profile_pic_url);
                            shareBtn.setVisibility(View.VISIBLE);
                            picPublish.setVisibility(View.VISIBLE);
                           /* shareBtn.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    shareOnFacebook(accessToken);                                }
                            });*/
                            picPublish.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    sharePicture(accessToken);
                                }
                            });

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
                Bundle parameters = new Bundle();
                parameters.putString("fields", "id,name,first_name,last_name,email,birthday,gender");
                graphRequest.setParameters(parameters);
                graphRequest.executeAsync();


            }

            @Override
            public void onCancel() {
                Utils.showToast(MainActivity.this, "User Cancelled Login");

            }

            @Override
            public void onError(FacebookException error) {
                Log.d("error", error.toString());
            }
        });
    }

    private void sharePicture(AccessToken accessToken) {
        Set<String> permissions = accessToken.getDeclinedPermissions();
        if (permissions.contains("publish_actions")) {
            LoginManager.getInstance().logInWithPublishPermissions(
                    MainActivity.this,
                    Arrays.asList("publish_actions"));
        } else {
            for (int i = 0; i < picList.size(); i++) {
                Bundle params = new Bundle();
                params.putString("message", first_name + last_name + "shared this");
                params.putString("url", picList.get(i));

                new GraphRequest(
                        AccessToken.getCurrentAccessToken(),
                        "/me/photos",
                        params,
                        HttpMethod.POST,
                        new GraphRequest.Callback() {
                            public void onCompleted(GraphResponse response) {
                                if (response.getError() != null) {
                                    Utils.showToast(MainActivity.this, response.getError().getErrorMessage());
                                } else {
                                    Utils.showToast(MainActivity.this, "pictures shared");
                                }
                            }
                        }
                ).executeAsync();
            }
        }
    }

    private void shareOnFacebook(AccessToken accessToken) {
        Set<String> permissions = accessToken.getDeclinedPermissions();
        if (permissions.contains("publish_actions")) {

        } else {
            Bundle params = new Bundle();
            params.putString("course", "http://samples.ogp.me/136756249803614");
            new GraphRequest(
                    AccessToken.getCurrentAccessToken(),
                    "/me/fitness.bikes",
                    params,
                    HttpMethod.POST,
                    new GraphRequest.Callback() {
                        public void onCompleted(GraphResponse response) {
                            if (response.getError() != null) {
                                Utils.showToast(MainActivity.this, response.getError().getErrorMessage());
                            } else {
                                Utils.showToast(MainActivity.this, "Shared");
                            }
                        }
                    }
            ).executeAsync();

        }

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }
}

1 个答案:

答案 0 :(得分:1)

您提到您已成功使用自己的帐户,但对另一个帐户失败了。在您上市之前,大多数权限(包括publish_actions)都需要经过Facebook审核。阅读文档中的登录审核:https://developers.facebook.com/docs/facebook-login/review

顺便说一下,当您授权具有扩展权限的应用程序作为App Admin时,应该会出现此警告。