2013-06-21 22 views
1

當我使用HelloFacebookSample時,代碼正常工作,我可以在用戶牆上發帖。android-facebook premission,Session:嘗試請求具有未決請求的會話的新權限

但是當我嘗試後視頻和embeded it on the wall,這樣做,我用publish to feed tutorial

當我轉換我的代碼替換postStatusUpdate() methood與publishStory()

public void postStatusUpdate() { 
     if (user != null && hasPublishPermission()) { 
      final String message ; 



       message = (user.getFirstName()+" "+ getString(R.string.status_update_link)+ " " +video_id + " " + getString(R.string.google_play_link)); 


      Request request = Request 
        .newStatusUpdateRequest(Session.getActiveSession(), message, new Request.Callback() { 
         @Override 
         public void onCompleted(Response response) { 
          showPublishResult(message, response.getGraphObject(), response.getError()); 
         } 
        }); 
      request.executeAsync(); 
     } else { 
      pendingAction = PendingAction.POST_STATUS_UPDATE; 
     } 
    } 

這種替換:

private void publishStory() { 
     Session session = Session.getActiveSession(); 

     if (session != null){ 

      // Check for publish permissions  
      List<String> permissions = session.getPermissions(); 
      if (!isSubsetOf(PERMISSIONS, permissions)) { 

       Session.NewPermissionsRequest newPermissionsRequest = new Session 
         .NewPermissionsRequest(this, PERMISSIONS); 
      session.requestNewPublishPermissions(newPermissionsRequest); 
       return; 
      } 

      Bundle postParams = new Bundle(); 



       postParams.putString("message", getString(R.string.google_play_link)); 
       postParams.putString("link", "http://www.youtube.com/watch?v="+ video_id); 
       postParams.putString("source", "http://www.youtube.com/v/" + video_id); 
       postParams.putString("picture","http://img.youtube.com/vi/" +video_id + "/0.jpg"); 





    Request.Callback callback= new Request.Callback() { 
     public void onCompleted(Response response) { 
      JSONObject graphResponse = response 
             .getGraphObject() 
             .getInnerJSONObject(); 
      String postId = null; 
      try { 
       postId = graphResponse.getString("id"); 
      } catch (JSONException e) { 
       Log.i(TAG, 
        "JSON error "+ e.getMessage()); 
      } 
      FacebookRequestError error = response.getError(); 
      if (error != null) { 


       Toast.makeText(MyApp.intense, 
        error.getErrorMessage(), 
        Toast.LENGTH_LONG).show(); 

       } else { 

       /** 
        Toast.makeText(MyApp.intense, 
         // postId 
          "Sent" 
         , 
         Toast.LENGTH_LONG).show(); 
       */   
      } 
     } 
    }; 

    Request request = new Request(session, "me/feed", postParams, 
          HttpMethod.POST, callback); 

    RequestAsyncTask task = new RequestAsyncTask(request); 
    task.execute(); 
} 

} 

所以當我嘗試發佈,應用程序是cresh,我收到「會話:試圖請求新的pe rmissions對於這種形式發佈的開發者收到了同樣的錯誤味精就像我接受,但它看起來像有一個懸而未決的要求」

我不覺得當第一權限的會話...

在其他Q這個錯誤出現在session is not OPENED和用戶調用它時,我相信這種情況不是問題。

我從這個FragmentActivity附加了更多的代碼,因爲我相信它是必要的。

private void onSessionStateChange(Session session, SessionState state, Exception exception) { 
     if (pendingAction != PendingAction.NONE && 
       (exception instanceof FacebookOperationCanceledException || 
       exception instanceof FacebookAuthorizationException)) { 
       new AlertDialog.Builder(HelloFacebookSampleActivity.this) 
        .setTitle(R.string.cancelled) 
        .setMessage(R.string.permission_not_granted) 
        .setPositiveButton(R.string.ok, null) 
        .show(); 
       postStatusUpdateButton.setEnabled(false); 
      pendingAction = PendingAction.NONE; 
     } else if (state == SessionState.OPENED_TOKEN_UPDATED) { 
      handlePendingAction(); 
     } 
     updateUI(); 
    } 




private enum PendingAction { 
     NONE, 

     POST_STATUS_UPDATE 
    } 
    private UiLifecycleHelper uiHelper; 

    private Session.StatusCallback callback = new Session.StatusCallback() { 
     @Override 
     public void call(Session session, SessionState state, Exception exception) { 
      onSessionStateChange(session, state, exception); 
     } 
    }; 
+0

我刪除所有的代碼,並從開始重新啓動,並解決問題。 – idan

回答

1

你可以只添加pendingPublishReauthorization = true;

//so 
if (session != null){ 

     // Check for publish permissions  
     List<String> permissions = session.getPermissions(); 
     if (!isSubsetOf(PERMISSIONS, permissions)) { 
    pendingPublishReauthorization = true; 

      Session.NewPermissionsRequest newPermissionsRequest = new Session 
        .NewPermissionsRequest(this, PERMISSIONS); 
     session.requestNewPublishPermissions(newPermissionsRequest); 
      return; 
     } 

它爲我工作。

希望這有助於

+0

感謝您的快速回答,但我嘗試這個,它不起作用 – idan

+1

請你可以附上所有的代碼? – idan

0

您對session.requestNewPublishPermissions(newPermissionsRequest);通話將產生一個網站對話框,要求用戶給你許可。您需要@Override onActivityResult()來捕獲並應用Facebook對話框中返回的內容。在我的應用我先問用戶進行身份驗證我的應用程序,然後請求允許訪問他們的組...

public class FacebookManage extends Activity { 
    // Facebook 
    private Session   fbSession  = null; 
    private int    authCount  = 0; 

    // There is a UI button that calls addAccount(); 

    public void addAccount(View v) { 
     // Open a new Session 
     OpenRequest or = new Session.OpenRequest(this); 
     or.setCallback(null); 
     // Login 
     Session.Builder sb = new Session.Builder(this); 
     Session session = sb.build(); 
     Session.setActiveSession(session); 
     // This will bring up a Facebook dialog and return a call to onActivityResult 
     session.openForRead(or); 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if (resultCode == RESULT_OK && requestCode == Session.DEFAULT_AUTHORIZE_ACTIVITY_CODE) { 
      if (authCount == 0) { 
       // This is the first authentication where we got basic access to the user account 
       Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data); 
       fbSession = Session.getActiveSession(); 
       if (fbSession != null && !fbSession.isClosed()) { 
        // Ask for Group permission 
        authCount++; 
        List<String> fbPermissions = new ArrayList<String>(); 
        fbPermissions.add("user_groups"); 
        NewPermissionsRequest snpr = new NewPermissionsRequest(this, fbPermissions); 
        fbSession.requestNewReadPermissions(snpr); 
       } 
      } else if (authCount == 1) { 
       // Returning from asking for access to "Groups" 
       if (fbSession != null && !fbSession.isClosed()) { 
        // This will add the group permission to the current session 
        fbSession.onActivityResult(this, requestCode, resultCode, data); 
       } 
      } 
     } 
    } 

其實我做三個回合請求的權限(初始,組,頁面和發佈)只需使用authCount來跟蹤我的位置。用戶看到4個Facebook對話框,但他們擁有的好東西只有一個「許可」請求。你有這個想法...

我剛剛撥完fbSession.close();。然後訪問令牌被Facebook SDK緩存和管理。通過使用一個活動來執行身份驗證並讓SDK跟蹤該活動,在其他活動(頁面)中,我只需要撥打fbSession = Session.openActiveSessionFromCache(this);,我很高興。

(注 - 如果用戶進入他們的Facebook的設置,特別核武器一些您最初要求的權限,你不會知道它,直到你嘗試和發佈啊,錯誤捕獲...)