问题描述
我正在尝试向当前用户发布一条墙消息。在我做请求之前,我正在验证会话并授权它是否存在。自从澄清以来,我遗漏了一些声明和变量的初始化。问题是这里显示的代码是在执行fbDidLogin-delegate方法之前执行的。因此,Facebook对象在请求之前似乎没有有效的访问令牌。我做了一个单身人士。下次我运行程序时它可以工作。
I am trying to post a wall message to the current user. Before I am doing the request I am validating the session and authorizing if it isn't. I left out some of the declarations and the initializing of the variables since to clarify. The problem is that the code shown here is executed before the fbDidLogin-delegate method is executed. So it seems like the Facebook object doesn't have the valid access token before it is requesting. I have made a singleton. Next time I run the program it works.
委托
- (void)fbDidLogin
{
NSLog(@"DEBUG: DID LOG IN");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
[defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
[defaults synchronize];
}
Viewcontroller
Viewcontroller
Facebook *facebook = [SocialMedia sharedFacebookAuthorized];
NSString *name = nil;
NSString *message = nil;
NSString *picture = nil;
if (self.didPassTest) {
// Code
} else {
// Code
}
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
name, @"name",
message, @"message",
picture, @"picture",
description, @"description",
link, @"link",
caption, @"caption", nil];
[facebook requestWithGraphPath:@"me/feed" andParams:params andHttpMethod:@"POST" andDelegate:self];
SharedFacebook..function
SharedFacebook..function
+ (Facebook *)sharedFacebookAuthorized
{
[self sharedFacebook];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"] && [defaults objectForKey:@"FBExpirationDateKey"]) {
facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}
NSArray *permissions = [[NSArray arrayWithObjects:@"email", @"read_stream", @"publish_stream", nil] retain];
NSLog(@"START?");
if (![facebook isSessionValid]) {
[facebook authorize:permissions];
NSLog(@"INSIDE?");
}
NSLog(@"CONTINUE?");
return facebook;
}
推荐答案
这是一项异步任务。在从图形API请求任何内容之前,您必须等待授权异步调用完成。请参阅我在此处对类似问题的回复:
This is an asynchronous task. You have to wait for the authorize asynchronous call to complete before requesting anything from the graph API. See my response to a similar question here: How to react to asynchronous events (login)?
这篇关于Facebook SSO并请求iOS SDK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!