需要扩展权限publish_actions

需要扩展权限publish_actions

问题描述:

我试图使用最新的facebook-sdk开发登录和共享图像,我成功实现了登录,但是当我尝试共享照片时,虽然我对publish_actions进行了检查,但并未提示我获得许可。错误出现为(#200)需要扩展权限publish_actions ..以下是我的代码。请帮助我什么,我做错了......任何帮助appreiciated ..需要扩展权限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); 
    } 
} 
+0

你想用应用程序管理员吗? – luschn

+0

nope我成功地使用我自己的帐户分享了图片,但其他帐户失败.. @ luschn –

+0

好的,那么只有一个答案,会在一分钟内添加。 – luschn

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

顺便说一句,当您授权具有扩展权限的应用程序作为应用程序管理员应该有关于此警告。

+0

我不能理解..当你授权一个具有扩展权限的应用程序作为应用程序管理员时,应该有一个警告。 –

+0

在登录弹出窗口中,通常会有一个警告。无论哪种方式,请阅读有关登录审查的内容,如果它与您自己的用户协同工作,但与其他用户协同工作,则无疑是个问题。 – luschn

+0

雅我高压弹出,但只有一次.. –