2016-06-21 82 views
1

如果不使用任何UI /按鈕,如何使用Facebook的Android SDK請求Facebook應用的發佈權限?我目前使用這段代碼,但它註冊了兩次回調,給我重複的結果。請求發佈權限的正確方法是什麼?如何使用Facebook的Android SDK請求發佈權限?

List<String> basicPermissions = Arrays.asList("user_friends", "user_games_activity"); 
    List<String> publishPermissions = Arrays.asList("publish_actions"); 

    mCallbackManager = CallbackManager.Factory.create(); 
    LoginManager.getInstance().logInWithReadPermissions(this, basicPermissions); 
    LoginManager.getInstance().logInWithPublishPermissions(this, publishPermissions); 

    LoginManager.getInstance().registerCallback(mCallbackManager, 
      new FacebookCallback<LoginResult>() { 
       @Override 
       public void onSuccess(LoginResult loginResults) { 

        new GraphRequest(
          AccessToken.getCurrentAccessToken(), 
          "/{my-app-id}/scores", 
          null, 
          HttpMethod.GET, 
          new GraphRequest.Callback() { 
           public void onCompleted(GraphResponse response) { 
            // my code 
           } 
          } 
        ).executeAsync(); 

       } 

       @Override 
       public void onCancel() { 

        Log.e("dd","facebook login canceled"); 

       } 

       @Override 
       public void onError(FacebookException e) { 



        Log.e("dd", "facebook login failed error"); 

       } 
      }); 

回答

0

根據FB,在用戶從讀取權限請求成功返回之前,您不應該請求發佈權限。所以,你必須改變它的東西,像這樣:

private boolean publishPermissionsRequested = false; 
private LoginResult readResultSuccess; 

private void loginWithFacebook(){ 
    LoginManager.getInstance().registerCallback(mCallbackManager, 
     new FacebookCallback<LoginResult>() { 
      @Override 
      public void onSuccess(LoginResult loginResults) { 
       if(!publishPermissionsRequested){ 
        publishPermissionsRequested = true; 
        readResultSuccess = loginResults; 

        LoginManager.getInstance() 
          .logInWithPublishPermissions(this, publishPermissions); 

        return; 
       } 

       onFacebookLoginSuccessful(loginResults); 
      } 

      @Override 
      public void onCancel() { 
       if(publishPermissionsRequested && readResultSuccess != null) 
        onFacebookLoginSuccessful(readResultSuccess); 
      } 

      @Override 
      public void onError(FacebookException e) { { 
       if(publishPermissionsRequested && readResultSuccess != null) 
        onFacebookLoginSuccessful(readResultSuccess); 
      } 
     }); 

    LoginManager.getInstance() 
      .logInWithReadPermissions(this, basicPermissions); 
} 

private void onFacebookLoginSuccessful(LoginResult loginResults){ 
    new GraphRequest(
      AccessToken.getCurrentAccessToken(), 
      "/{my-app-id}/scores", 
      null, 
      HttpMethod.GET, 
      new GraphRequest.Callback() { 
       public void onCompleted(GraphResponse response) { 
         // Your code 
       } 
      } 
    ).executeAsync(); 
} 

這樣,如果他們接受的讀取權限,但拒絕它仍將繼續作爲一個成功(或失敗出)發佈的權限請求。

希望這會爲您指出正確的方向。

相關問題