2014-07-17 142 views
0

我試圖以管理員身份發佈到facebook頁面。該帖子總是以用戶身份出現,而不是該帖子由頁面持有。作爲頁面發佈在facebook頁面上不是用戶

這裏是代碼,我參加了一個樣品上張貼的用戶牆,並添加manages_page權限和改變我/飼料PAGE_ID

Java文件:

public class MainFragment extends Fragment { 

private static final String TAG = "MainFragment"; 

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

private Button shareButton; 

private static final List<String> PERMISSIONS = Arrays.asList("publish_actions"); 


private static final String PENDING_PUBLISH_KEY = "pendingPublishReauthorization"; 

private boolean pendingPublishReauthorization = false; 
TextView text; 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.main, container, false); 
    text = (TextView)view.findViewById(R.id.textView1); 
    LoginButton authButton = (LoginButton) view 
      .findViewById(R.id.authButton); 
    authButton.setFragment(this); 
    authButton.setPublishPermissions("manage_pages"); 
    shareButton = (Button) view.findViewById(R.id.shareButton); 
    shareButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      publishStory(); 
     } 
    }); 

    if (savedInstanceState != null) { 
     pendingPublishReauthorization = savedInstanceState.getBoolean(
       PENDING_PUBLISH_KEY, false); 
    } 
    return view; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    uiHelper = new UiLifecycleHelper(getActivity(), callback); 
    uiHelper.onCreate(savedInstanceState); 
} 

@Override 
public void onResume() { 
    super.onResume(); 

    // For scenarios where the main activity is launched and user 
    // session is not null, the session state change notification 
    // may not be triggered. Trigger it if it's open/closed. 
    Session session = Session.getActiveSession(); 

    if (session != null && (session.isOpened() || session.isClosed())) { 
     onSessionStateChange(session, session.getState(), null); 
    } 

    uiHelper.onResume(); 
} 

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

@Override 
public void onPause() { 
    super.onPause(); 
    uiHelper.onPause(); 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    uiHelper.onDestroy(); 
} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    outState.putBoolean(PENDING_PUBLISH_KEY, pendingPublishReauthorization); 
    uiHelper.onSaveInstanceState(outState); 
} 

private void onSessionStateChange(Session session, SessionState state, 
     Exception exception) { 
    if (state.isOpened()) { 
     shareButton.setVisibility(View.VISIBLE); 
     if (pendingPublishReauthorization 
       && state.equals(SessionState.OPENED_TOKEN_UPDATED)) { 
      pendingPublishReauthorization = false; 
      publishStory(); 
     } 
    } else if (state.isClosed()) { 
     shareButton.setVisibility(View.INVISIBLE); 
    } 
} 

private void publishStory() { 
    final 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); 


     // session.open(session.getAccessToken(), callback); 
      return; 
     } 

     final Bundle postParams = new Bundle(); 
     postParams.putString("message", "Testing app"); 


     final 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(getActivity().getApplicationContext(), 
          error.getErrorMessage(), Toast.LENGTH_SHORT) 
          .show(); 
       } else { 
        Toast.makeText(getActivity().getApplicationContext(), 
          postId, Toast.LENGTH_LONG).show(); 
       } 
      } 
     }; 

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

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

private boolean isSubsetOf(Collection<String> subset, 
     Collection<String> superset) { 
    for (String string : subset) { 
     if (!superset.contains(string)) { 
      return false; 
     } 
    } 
    return true; 
} 

} 
+0

嗨,我已經用你的代碼,但登錄後,我收到提示 java.lang.UnsupportedOperationException:會話:有人企圖爲具有未決請求的會話請求新權限。在session.requestNewPublishPermissions(newPermissionsRequest)上的 ; – Achin

回答

1

顯然你不獲取頁面訪問令牌,並使用用戶訪問令牌代替。請求manage_pages權限是做正確的事,但事後你需要撥打電話到

/me/accounts 

拿到個人頁面的訪問令牌。然後,你必須使用此訪問令牌,使您的要求

{page_id}/feed 
+0

你能提供一個示例代碼,因爲我沒有捕獲它如何做到這一點 –

+0

嗨,我已經使用你的代碼,但登錄後,我得到錯誤java.lang.UnsupportedOperationException:會話:試圖請求新具有未決請求的會話的權限。 on session.requestNewPublishPermissions(newPermissionsRequest); – Achin

相關問題