2014-02-07 82 views
3

我是facebook開發的新手。我想要loginfacebook,並允許用戶允許我的應用發佈照片和內容。但是,當用戶login成功他顯示授權對話框twice.here是我的代碼授權對話框登錄Facebook時出現兩次android

Session.openActiveSession(this, true, new Session.StatusCallback() { 

      // callback when session changes state 
      @Override 
      public void call(Session session, SessionState state, 
        Exception exception) { 

       if (session.isOpened()) { 
        Log.d("", "usman: session is opened"); 
        // make request to the /me API 

        Request.executeMeRequestAsync(session, 
          new Request.GraphUserCallback() { 

           // callback after Graph API response with 
           // user 
           // object 
           @Override 
           public void onCompleted(GraphUser user, 
             Response response) { 
            Log.d("", "onCompleted called"); 
            if (user != null) { 
             // TextView welcome = (TextView) 
             // findViewById(R.id.welcome); 
             Log.d(MyConstants.TAG, 
               "facebook Hello : " 
                 + user.getName() + "!"); 

             publishStory(); 
             //isShared = true; 

            } else { 
             Toast.makeText(
               getApplicationContext(), 
               "Unable to Post,Try Again later", 
               Toast.LENGTH_LONG).show(); 
            } 
           } 
          }); 

       } else { 
        Log.d("", "usman: Session is not open"); 
       } 
      } 
     }); 

private void publishStory() { 
     Log.d("", "usman: in publishStory"); 
     Session session = Session.getActiveSession(); 

     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; 
      } 

      FileInputStream fis = null; 
      try { 
       fis = new FileInputStream(fileImage); 
      } catch (FileNotFoundException e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      } 
      Bitmap bi = BitmapFactory.decodeStream(fis); 
      ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
      bi.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
      byte[] data = baos.toByteArray(); 

      Bundle postParams = new Bundle(); 

      postParams.putString("name", "London For Less Postcard"); 
      postParams.putString("caption", 
        "Build great social apps and get more installs."); 
      postParams 
        .putString(
          "description", 
          "The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps."); 

      postParams.putByteArray("picture", data); 

      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("FB", "JSON error " + e.getMessage()); 
        } 
        FacebookRequestError error = response.getError(); 
        if (error != null) { 
         Toast.makeText(
           SendPostCardActivity.this 
             .getApplicationContext(), 
           error.getErrorMessage(), Toast.LENGTH_SHORT) 
           .show(); 
         Log.d("", "usman: Error"); 
        } else { 
         Toast.makeText(
           SendPostCardActivity.this 
             .getApplicationContext(), 
           "Posted on facebook wall successfully", 
           Toast.LENGTH_LONG).show(); 
         Log.d("", "usman: Finish"); 
         finish(); 
        } 
       } 
      }; 

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

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

    } 
+0

從您的代碼,這似乎是預期的結果。首先調用openActiveSession,它將顯示auth對話框。然後,獲得初始權限後,您調用publishStory,它將請求新的發佈權限,並且它將再次顯示auth對話框。 –

回答

0

我解決了它這樣的:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    callbackManager.onActivityResult(requestCode, resultCode, data); 
    if (AccessToken.getCurrentAccessToken() != null){ 
     if (!AccessToken.getCurrentAccessToken().getPermissions().contains("publish_actions")){ 
      LoginManager.getInstance().logInWithPublishPermissions(MainActivity.this, Arrays.asList("publish_actions")); 
     } 
    } 
} 
+0

但它第一次會顯示兩個登錄權限,一個具有普通權限,另一個具有發佈權限 – Hanuman

1

對我來說也是Facebook登錄對話框中顯示兩次,當我點擊「登錄「Facebook按鈕。

我剛剛更改了Facebook的默認按鈕,並使用了正常按鈕,並帶有Facebook按鈕背景,現在登錄頁面只顯示一次。

Facebook默認按鈕顯示正常和發佈權限的登錄頁面。我不知道細節,但這對我有用。