0

我在从网络上为我正在编写的 Facebook Feed 应用程序提取 Facebook 访问令牌时遇到了一些问题。这个问题与获得 Facebook 令牌并不严格相关;这只是框架的问题。当我转到 https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=[APP_ID]&client_secret=[APP_SECRET] 时,我在一个页面上返回了一个令牌,上面写着:

access_token=464483653570261|cY9NHFBWCDJ9hSQfswWFg0FDZvw

如何将网页中的内容解析到我的应用程序中?我对Objective C比较陌生(而且我只有一年的基本编码经验),所以我尝试使用我在网上找到的一种方法来获取JSON提要,结合一个简单的解析方法,但是它没有用。代码如下:

    id getToken = [self objectWithUrl:[NSURL URLWithString:@"https://graph.facebook.com/
                           oauth/access_token?grant_type=client_credentials&
                           client_id=464483653570261&
                           client_secret=55bb8395ed0293bf37af695f6cdaa1fb"]];
    NSString *fullToken = (NSString *)getToken;
    NSLog(@"fullToken: %@", fullToken);
    NSArray *components = [fullToken componentsSeparatedByString:@"="];
    NSString *token = [components objectAtIndex:1];
    NSLog(@"token: %@", token);

我的两个 NSLogs 都说各自的字符串指向(空)。我不确定我做错了什么,而且我在互联网上找到答案的运气并不好,因为我不知道该怎么称呼我正在尝试做的事情。如果您有任何帮助或替代方法,我将不胜感激。

4

2 回答 2

3

从外观上看,你得到的值不是 JSON,它只是一个字符串。

尝试这样的事情:

NSURL * url = [NSURL URLWithString:@"https://graph.facebook.com/
                       oauth/access_token?grant_type=client_credentials&
                       client_id=464483653570261&
                       client_secret=55bb8395ed0293bf37af695f6cdaa1fb"]];
NSString * fullToken = [NSString stringWithContentsOfUrl: url];
NSLog(@"fullToken: %@", fullToken);
NSArray *components = [fullToken componentsSeparatedByString:@"="];
NSString *token = [components objectAtIndex:1];
NSLog(@"token: %@", token);
于 2012-07-30T19:37:04.713 回答
0

有一种更简单的方法可以使用 ACAccountStore 和 ACAccountType 获取用户的 access_token。检查下面的完整代码:

ACAccountStore *accountStore = [[ACAccountStore alloc] init];
ACAccountType *accountType =  [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSDictionary *FBOptions = [NSDictionary dictionaryWithObjectsAndKeys:FACEBOOK_APP_ID, ACFacebookAppIdKey,@[@"email"],ACFacebookPermissionsKey, nil];

[accountStore requestAccessToAccountsWithType:accountType options:FBOptions completion:
 ^(BOOL granted, NSError *error) {
     if (granted) {

         NSArray *facebookAccounts = [accountStore accountsWithAccountType:accountType];
         FBAccount = [facebookAccounts firstObject];
         NSLog(@"token :%@",[[FBAccount credential] oauthToken]);

     } else {
         NSLog(@"error getting permission %@",error);
         if([error code]== ACErrorAccountNotFound){
             NSLog(@"Account not found. Please setup your account in settings app"); 
         }
         else {
             NSLog(@"Account access denied");
         }

     }
 }];
于 2016-04-13T12:49:21.093 回答