我跟随facebook(https://developers.facebook.com/docs/android/share)关注facebook android共享教程。该应用程序运行正常,我可以登录Facebook点击演示共享按钮。所以这是我的应用程序:
user_likes, user_friends, user_status, basic_info
publish_actions
的附加权限的请求以下代码执行此请求:
public class MainActivity extends Activity {
private Button shareButton;
private UiLifecycleHelper uiHelper;
private static final List<String> PERMISSIONS = Arrays.asList("publish_actions");
private static final String PENDING_PUBLISH_KEY = "pendingPublishReauthorization";
private boolean pendingPublishReauthorization = false;
...
private void publishStory() {
Session session = Session.getActiveSession();
if( session != null) {
List<String> permissions = session.getPermissions();
if(!isSubsetOf(PERMISSIONS, permissions)) {
pendingPublishReauthorization = true;
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(this, PERMISSIONS);
//Request the new permission here. The answer is processed in onSessionStateChange() method
session.requestNewPublishPermissions(newPermissionsRequest);
return;
}
...
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
//since the session is already open we enter this if branch
if (state.isOpened()) {
shareButton.setVisibility(View.VISIBLE);
//The result of state.equals(SessionState.OPENED_TOKEN_UPDATED) is false which indicates that the session object has not been updated (as far as I understand the fb api)
Log.e(TAG, "state.equals(SessionState.OPENED_TOKEN_UPDATED) = " + state.equals(SessionState.OPENED_TOKEN_UPDATED));
//Output all the current permission the session has (publish_actions is not included here)
List<String> permissions = session.getPermissions();
Iterator<String> it = permissions.iterator();
Log.e(TAG, "Content of list:");
while(it.hasNext())
Log.e(TAG, "permission = " + it.next());
//Since the OPENED_TOKEN_UPDATED has not been updated nothing further happens here
if (pendingPublishReauthorization && state.equals(SessionState.OPENED_TOKEN_UPDATED)) {
pendingPublishReauthorization = false;
publishStory();
}
} else if (state.isClosed()) {
shareButton.setVisibility(View.INVISIBLE);
}
}
所以,虽然这似乎是正确的,但必定存在一些错误。未授予publish_actions权限,但我不明白为什么。只有现有权限保留publish_actions
。
进一步说明:我已经阅读了一些问题,说明应该出现一个额外的弹出窗口,要求用户授予权限。然而,在我的情况下,这不会发生。也许这就是问题?如果是这样,我需要做什么才能获得弹出窗口?
更新:我还从github尝试了以下要点:https://gist.github.com/vishalpawale/5556996 由于完全相同的原因,此示例代码失败。它没有获得任何publish_action权限。似乎是Facebook SDK中的实际错误?
答案 0 :(得分:1)
对于发布权限,您应该在应用设置中发送请求并发送以供审核,请参阅https://developers.facebook.com/docs/apps/review/
答案 1 :(得分:0)
如果您使用Facebook sdk 3.14.x +,那么在4月30日之后,facebook已经更改了应用程序测试和提交过程。发布有关许可相关的答案。查看this链接
答案 2 :(得分:0)
我们在这里遇到了同样的问题。
尝试使用:
Session.getActiveSession().refreshPermissions();
这将刷新
上可用的权限列表List<String> permissions = session.getPermissions();
它仅适用于Facebook注册的开发者用户或通过Facebook App Dashboard注册的测试用户。
这对我们有用。
答案 3 :(得分:0)
它只返回读权限,而不是写权限。试试下面的代码段
final Bundle permBundle = new Bundle();
permBundle.putCharSequence("permission", "publish_actions");
GraphRequest request = new GraphRequest(
AccessToken.getCurrentAccessToken(),
"/me/permissions", permBundle, HttpMethod.GET,
new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse graphResponse) {
Log.d(TAG, "response2: " + graphResponse.getJSONObject());
try {
JSONArray permList = (JSONArray) graphResponse.getJSONObject().get("data");
if(permList.length() == 0){
// no data for perms, hence asking permission
askForFBPublishPerm();
}else{
JSONObject permData = (JSONObject) permList.get(0);
String permVal = (String) permData.get("status");
if(permVal.equals("granted")){
postToFB();
}else{
askForFBPublishPerm();
}
}
} catch (JSONException e) {
Log.d(TAG, "exception while parsing fb check perm data" + e.toString());
}
}
}
);
request.executeAsync();