iphone - 我在 iOS 6 中获得了 Facebook 的格式错误的访问 token

标签 iphone ios objective-c xcode facebook

我想获取 facebook 好友的个人资料图片。我还为其提供了 access_token。

我在网上搜索了很多,但没有找到任何好的解决方案。

我的代码如下:

- (IBAction)advancedButtonPressed:(id)sender {

    self.accountStore = [[ACAccountStore alloc]init];
    ACAccountType *FBaccountType= [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

    //NSString *key = @"xxxxxx";

    NSString *key = @"xxxxxx";
    NSDictionary *dictFB = [NSDictionary dictionaryWithObjectsAndKeys:key,ACFacebookAppIdKey,@[@"email"],ACFacebookPermissionsKey, nil];


        [self.accountStore requestAccessToAccountsWithType:FBaccountType options:dictFB completion:
         ^(BOOL granted, NSError *e) {
             if (granted) {
                 NSArray *accounts = [self.accountStore accountsWithAccountType:FBaccountType];
                 //it will always be the last object with single sign on
                 self.facebookAccount = [accounts lastObject];
                    NSLog(@"facebook account =%@",self.facebookAccount);

                 ACAccountCredential *fbCredential = [self.facebookAccount credential];
                accessToken = [fbCredential oauthToken];
                 NSLog(@"Facebook Access Token: %@", accessToken);

                 [self get];
             } else {
                 //Fail gracefully...
              NSLog(@"error getting permission %@",e);

             }
         }];

}


-(void)fr
{
NSURL *requestURL = [NSURL URLWithString:@"https://graph.facebook.com/me/friends?fields=id,name,picture,first_name,last_name,gender&access_token=BAAFcRQZCZAUeUBAICacM2q1V5UzRHee6xHGnbEdu19v6CZBIEDJmTwHBKtMZBwtVuFiEeuXqt4yKeSkeI17QZBGaZCeszBfOKdbZAfR4E9RN2nFqHalKPXZBga96WQCU8CSNG4ZCJCK1V7JIbqZAPRgNl6mjjO4Ns1CEaLpbXUYtum1CXbNqTtT82YpVVKoZCmMEBpqHfwYMx1OKu9RZBsjPZA6DlhKA1uGyU32EGJ8QOQZASJVQZDZD"];

    SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                            requestMethod:SLRequestMethodGET
                                                      URL:requestURL
                                               parameters:nil];
    request.account = self.facebookAccount;



    [request performRequestWithHandler:^(NSData *data,
                                         NSHTTPURLResponse *response,
                                         NSError *error) {

        if(!error)
        {
            list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

            NSLog(@"Dictionary contains data: %@", list );
            if([list objectForKey:@"error"]!=nil)
            {
                [self attemptRenewCredentials];
            }
            dispatch_async(dispatch_get_main_queue(),^{
                nameLabel.text = [list objectForKey:@"username"];
            });
        }
        else{
            //handle error gracefully
            NSLog(@"error from get%@",error);
            //attempt to revalidate credentials
        }

    }];
}

但我总是得到这样的错误:

error =     {
        code = 190;
        message = "Malformed access token BAAFcRQZCZAUeUBAICacM2q1V5UzRHee6xHGnbEdu19v6CZBIEDJmTwHBKtMZBwtVuFiEeuXqt4yKeSkeI17QZBGaZCeszBfOKdbZAfR4E9RN2nFqHalKPXZBga96WQCU8CSNG4ZCJCK1V7JIbqZAPRgNl6mjjO4Ns1CEaLpbXUYtum1CXbNqTtT82YpVVKoZCmMEBpqHfwYMx1OKu9RZBsjPZA6DlhKA1uGyU32EGJ8QOQZASJVQZDZD?access_token=BAAFcRQZCZAUeUBAHBZBSdTL9wWFDOVthxj78bBJlrNEkPiKHdxZA1HaRmO9dtBTJdsZCtZCu2vaE5ByZAdK7Ox30BpIR0KTg0ZC6pi1IOE0KnZCZBSxqbycG0C6Ws5Ct4ferL3G0VU1wIfDypKekFM3zB4r5gAiUREQ66Yuz2Fu2mp5NGqZCBE1p357H41P2lKOAQN9EZAmu4q3SRtjMuVIw4dtdC00l4RhMuDWTOIUQlO54bgZDZD";
        type = OAuthException;
    };
}

我无法解决这个问题。帮帮我!

最佳答案

如果您使用的是 SLRequest,它会自动为您附加访问 token ,并且您也不应该在 URL 本身中包含任何请求参数。相反,你应该这样做:

NSURL *requestURL = [NSURL URLWithString:@"https://graph.facebook.com/me/friends"];

SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                        requestMethod:SLRequestMethodGET
                                                  URL:requestURL
                                           parameters:@{@"fields":@"id,name,picture,first_name,last_name,gender"}];

关于iphone - 我在 iOS 6 中获得了 Facebook 的格式错误的访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16442238/

相关文章:

iphone - xcode 发行版配置文件 "None"

iphone - 如何知道任务是否在后台执行

iphone - 设计一个类似 Flipboard 的 iOS 应用程序需要什么?

objective-c - GKTurnBasedMatch endTurnWithNextParticipants :turnTimeout:matchData:completionHandler: 问题

ios - iOS 应用程序平均在后台运行多长时间,直到它被杀死?

iphone - 丹·布里克林(Dan Briklin)的笔记记录器怎么能画得这么流畅?

ios - 在NSInteger的情况下设置 key 的值

ios - 如何有条件地使 StackView 中的图像居中

objective-c - 在 Cocoa 中编写我自己的 @dynamic 属性

objective-c - 我如何配置 CocoaPods 以使用 https ://github. com/RestKit/cocoa-oauth 而不是过时的?