我最近开始使用 Facebook SDK 3.1,在使用openActiveSessionWithReadPermissions
.
实际上,login in 工作得很好,如果有可用的缓存令牌,它会在不显示 Facebook UI 的情况下登录,如果没有,它会显示 UI。
在我拨打reauthorizeWithPublishPermissions
. 如果我调用reauthorizeWithPublishPermissions
,然后关闭并重新打开应用程序,然后调用openActiveSessionWithReadPermissions
,它会显示 Facebook UI 并要求用户说“是的,我可以读取权限”,即使有可用的缓存令牌。
如果我拨打电话,它只会错误地显示 Facebook UI reauthorizeWithPublishPermissions
,否则一切正常。
打开阅读代码:
[FBSession openActiveSessionWithReadPermissions:readpermissions allowLoginUI:YES
completionHandler:^(FBSession *aSession, FBSessionState status, NSError *error) {
[self sessionStateChanged:[FBSession activeSession] state:status error:error];
if (status != FBSessionStateOpenTokenExtended) {
// and here we make sure to update our UX according to the new session state
FBRequest *me = [[FBRequest alloc] initWithSession:aSession
graphPath:@"me"];
[me startWithCompletionHandler:^(FBRequestConnection *connection,
NSDictionary<FBGraphUser> *aUser,
NSError *error) {
self.user = aUser;
aCompletionBlock(aSession, status, error);
}];
}
}];
sessionStateChanged 函数:
- (void)sessionStateChanged:(FBSession *)aSession state:(FBSessionState)state error:(NSError *)error {
if (aSession.isOpen) {
// Initiate a Facebook instance and properties
if (nil == self.facebook || state == FBSessionStateOpenTokenExtended) {
self.facebook = [[Facebook alloc]
initWithAppId:FBSession.activeSession.appID
andDelegate:nil];
// Store the Facebook session information
self.facebook.accessToken = FBSession.activeSession.accessToken;
self.facebook.expirationDate = FBSession.activeSession.expirationDate;
}
} else {
// Clear out the Facebook instance
if (state == FBSessionStateClosedLoginFailed) {
[FBSession.activeSession closeAndClearTokenInformation];
}
self.facebook = nil;
}
}
Publish 调用,带有一个空的 aPublishAction 用于测试:
- (void)doPublishAction:(void(^)(FBSession *aSession, NSError *error))aPublishAction {
if ([FBSession.activeSession.permissions
indexOfObject:@"publish_actions"] == NSNotFound) {
NSArray *writepermissions = [[NSArray alloc] initWithObjects:
@"publish_stream",
@"publish_actions",
nil];
[[FBSession activeSession]reauthorizeWithPublishPermissions:writepermissions defaultAudience:FBSessionDefaultAudienceFriends completionHandler:^(FBSession *aSession, NSError *error){
if (error) {
NSLog(@"Error on public permissions: %@", error);
}
else {
aPublishAction(aSession, error);
}
}];
}
else {
// If permissions present, publish the story
aPublishAction(FBSession.activeSession, nil);
}
}
提前感谢您所做的一切,我将不胜感激任何和所有的帮助!