以下是我在Facebook上分享应用的完整源代码,
private static final List<String> PERMISSIONS = Arrays
.asList("publish_actions");
private static final String PENDING_PUBLISH_KEY = "pendingPublishReauthorization";
private boolean pendingPublishReauthorization = false;
private UiLifecycleHelper uiHelper;
Session.StatusCallback callback = new Session.StatusCallback()
{
@Override
public void call(Session session, SessionState state,
Exception exception)
{
if (pendingPublishReauthorization
&& state.equals(SessionState.OPENED_TOKEN_UPDATED))
{
pendingPublishReauthorization = false;
shareThisLevelOnFacebook();
}
else if (session.isOpened())
{
shareThisLevelOnFacebook();
}
}
};
private boolean isLoggedInFacebook()
{
Session session = Session.getActiveSession();
if (session != null && session.isOpened())
return true;
else
return false;
}
public void shareThisLevelOnFacebook()
{
if(curWeatherBitmap == null) return;
boolean isLoggedIn = isLoggedInFacebook();
if (!isLoggedIn)
{
// start Facebook Login
Session.openActiveSession(this, true, callback);
}
else
{
// Check for publish permissions
List<String> permissions = Session.getActiveSession()
.getPermissions();
if (!isSubsetOf(PERMISSIONS, permissions))
{
pendingPublishReauthorization = true;
Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(
this, PERMISSIONS);
Session.getActiveSession().requestNewPublishPermissions(
newPermissionsRequest);
return;
}
Request reqPost = Request.newUploadPhotoRequest(
Session.getActiveSession(), curWeatherBitmap, null);
//Parameters-info can be found on https://developers.facebook.com/docs/reference/api/post/
Bundle postParams = reqPost.getParameters();
StringBuilder sb = new StringBuilder();
sb.append(" Default image title");
postParams.putString("name", sb.toString());
reqPost.setParameters(postParams);
reqPost.setCallback(new Callback()
{
@Override
public void onCompleted(Response response)
{
FacebookRequestError error = response.getError();
if (error == null)
{
Toast.makeText(WeatherDetailActivity.this,
"Posted on Facebook has been finished",
Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(WeatherDetailActivity.this,
"Something went wrong", Toast.LENGTH_SHORT)
.show();
Log.d(getClass().getCanonicalName(),
error.getErrorMessage());
}
}
});
reqPost.executeAsync();
}
}
当设备安装了Facebook应用时,哪种方法正常。但如果没有,
List<String> permissions = Session.getActiveSession()
.getPermissions();
始终返回一个空权限列表,因此它会创建一种循环(权限请求,权限列表为空,再次请求)。
答案 0 :(得分:0)
您可以明确刷新权限。
Session s = Session.getActiveSession();
s.refreshPermissions();
这将导致您的权限从服务器刷新并在状态回调中更新。这应该是授予权限的权威来源。