如果您需要延長權限,您可以在這裏關注guide。從帖子:
讓我們先定義什麼權限,我們將需要
// List of additional write permissions being requested
private static final List<String> PERMISSIONS = Arrays.asList("publish_stream, publish_actions");
// Request code for reauthorization requests.
private static final int REAUTH_ACTIVITY_CODE = 100;
// Flag to represent if we are waiting for extended permissions
private boolean pendingAnnounce = false;
我們將首先檢查,如果我們有所需的所有權限。如果不是,我們用我們請求的一組權限創建一個新的ReauthorizeRequest。這會將我們的應用以外的用戶引導至Facebook應用或瀏覽器以確認權限。當用戶確認請求時,他被重定向回到我們的應用程序,onActivityResult調用了重新授權請求被調用的Activity。
private void handleAnnounce() {
pendingAnnounce = false;
Session session = Session.getActiveSession();
if (session == null || !session.isOpened()) {
return;
}
List<String> permissions = session.getPermissions();
if (!permissions.containsAll(PERMISSIONS)) {
pendingAnnounce = true; // Mark that we are currently waiting for confirmation of publish permissions
session.addCallback(this);
requestPublishPermissions(this, session, PERMISSIONS, REAUTH_ACTIVITY_CODE);
return;
}
// TODO: Publish the post. You would need to implement this method to actually post something
publishMessage();
}
void requestPublishPermissions(Activity activity, Session session, List<String> permissions,
int requestCode) {
if (session != null) {
Session.ReauthorizeRequest reauthRequest = new Session.ReauthorizeRequest(activity, permissions)
.setRequestCode(requestCode);
session.reauthorizeForPublish(reauthRequest);
}
}
如果會話狀態更改,則會處理結果並調用onSessionStateChanged。然後,如果我們的令牌已成功更新並嘗試再次發佈該消息,則檢查該方法。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REAUTH_ACTIVITY_CODE:
Session session = Session.getActiveSession();
if (session != null) {
session.onActivityResult(this, requestCode, resultCode, data);
}
break;
}
}
protected void onSessionStateChange(final Session session, SessionState state, Exception exception) {
if (session != null && session.isOpened()) {
if (state.equals(SessionState.OPENED_TOKEN_UPDATED)) {
// Session updated with new permissions
// so try publishing once more.
tokenUpdated();
}
}
}
/**
* Called when additional permission request is copmleted successfuly.
*/
private void tokenUpdated() {
// Check if a publish action is in progress
// awaiting a successful reauthorization
if (pendingAnnounce) {
// Publish the action
handleAnnounce();
}
}