我正在使用facebook sdk在facebook上分享文字,但在尝试登录时无法获取访问令牌。登录成功完成但没有获得任何访问令牌。 因此,如果没有访问令牌,我无法在Facebook上分享文本。 我的代码如下
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:@[@"public_profile"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if ([result.token hasGranted:@"publish_actions"]) {
[[[FBSDKGraphRequest alloc]
initWithGraphPath:@"me/feed"
parameters: @{ @"message" : @"hello world"}
HTTPMethod:@"POST"]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(@"Post id:%@", result[@"id"]);
}
}];
}
if (error) {
// Process error
} else if (result.isCancelled) {
// Handle cancellations
} else {
// If you ask for multiple permissions at once, you
// should check if specific permissions missing
if ([result.grantedPermissions containsObject:@"email"]) {
// Do work
}
}
}];
我在FBSDKLoginManager对象的处理程序块中得到结果而没有错误,但它不包含令牌或其他数据
输出如下
请告诉我如何解决这个问题 感谢
答案 0 :(得分:4)
尝试此代码。基于Facebook SDK 4.0版
<强> AppDalegate.m 强>
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
return [[FBSDKApplicationDelegate sharedInstance] application:application
openURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
<强> ViewController.m 强>
- (IBAction)btnFacebookPressed:(id)sender {
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
{
if (error)
{
// Process error
}
else if (result.isCancelled)
{
// Handle cancellations
}
else
{
if ([result.grantedPermissions containsObject:@"email"])
{
NSLog(@"result is:%@",result);
[self fetchUserInfo];
[login logOut];
}
}
}];
}
-(void)fetchUserInfo
{
if ([FBSDKAccessToken currentAccessToken])
{
NSLog(@"Token is available : %@",[[FBSDKAccessToken currentAccessToken]tokenString]);
[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, name, link, first_name, last_name, picture.type(large), email, birthday, bio ,location ,friends ,hometown , friendlists"}]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error)
{
NSLog(@"resultis:%@",result);
}
else
{
NSLog(@"Error %@",error);
}
}];
}
}