3

我已经实现了 facebook SDK,借助它能够使用以下代码获取 facebook 身份验证以及用户信息。

  if(!self.accountStore)
    self.accountStore = [[ACAccountStore alloc] init];
if(NSClassFromString(@"SLComposeViewController") != nil)
{
    ACAccountType *facebookTypeAccount = [self.accountStore 
    accountTypeWithAccountTypeIdentifier:@"com.apple.facebook"];
    NSLog(@"facebookTypeAccount..:%@",facebookTypeAccount);
    NSArray *accounts = [self.accountStore accountsWithAccountType:facebookTypeAccount];

    if (facebookTypeAccount) {
        if ([accounts respondsToSelector:@selector(count)]) {
            [self.accountStore requestAccessToAccountsWithType:facebookTypeAccount
                                                       options:@{ACFacebookAppIdKey:  
                                     @"key", ACFacebookPermissionsKey: @[@"email"]}
                                                    completion:^(BOOL granted, NSError 
                                                                             *error) {
              if(granted){
            NSArray *accounts = [self.accountStore 
                                       accountsWithAccountType:facebookTypeAccount];
                        [self fetchuserinfo];

                                                            }

                                                    }];

        }



        -(void)fetchuserinfo
         {
        NSString *accessToken = [FBSession activeSession].accessToken;
       NSString *url=[NSString stringWithFormat:@"https://graph.facebook.com/me"];
        NSURL *meurl = [NSURL URLWithString:url];
       SLRequest *merequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                          requestMethod:SLRequestMethodGET
                                                    URL:meurl
                                             parameters:nil];

NSLog(@" setting account %@",_facebookAccount);

merequest.account = _facebookAccount;

[merequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
    NSString *meDataString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    returnedData = responseData;

    NSLog(@" meRequest Respnse %@", meDataString);
    if (returnedData!=nil) {
        if(NSClassFromString(@"NSJSONSerialization"))
        {
            NSError *error = nil;
            id object = [NSJSONSerialization
                         JSONObjectWithData:returnedData
                         options:0
                         error:&error];

            if(error) {  }


            if([object isKindOfClass:[NSDictionary class]])
            {
                NSDictionary *results = object;

                NSString *categoryString=[[results valueForKeyPath:@"picture.data"] objectForKey:@"url"];

                NSString *userid=[results objectForKey:@"id"];
                if (userid != nil)
                {
                    self.gotUserDetails = YES;
                }
                NSString *username=[results objectForKey:@"name"];
                NSString *email=[results objectForKey:@"email"];

               NSUserDefaults *userId=[NSUserDefaults standardUserDefaults];
                [userId setObject:userid forKey:@"userid"];


            }

        }
    }
    }

但是要从这个登录用户发布提要(https://graph.facebook.com/me/feed/access_token=),我需要访问令牌。如果有人知道如何在 iOS 6.0 中获取访问令牌,请帮助我。

谢谢大家。

4

3 回答 3

0

iOS 7 和 iOS 8 中的 facebook SDK 3.2 对我来说很好用。

你应该导入 FacebookSDK/FacebookSDK.h

#import <FacebookSDK/FacebookSDK.h>
....
[self checkFBIDWithToken:[FBSession activeSession].accessTokenData.accessToken];

PS:“checkFBIDWithToken:(NSString *)tempToken”是一种方法。

脸书文档:accessToken

于 2014-11-11T02:40:51.800 回答
0

SLRequestApple 提供了您可以使用的令牌,而不是担心获取令牌并将其传递给您。您指定目标 URL 和参数,并SLRequest(透明地)处理响应数据的身份验证、传输和收集。


您可能实际上没有获得令牌,但您仍然需要获得授权,并且该授权与您想要做的事情列表相关联。您的代码当前仅显示您想要获取电子邮件地址,但您可以添加其他权限:

[accountStore requestAccessToAccountsWithType:facebookAccountType
                                          options:
     @{
                              ACFacebookAppIdKey : ...,
                        ACFacebookPermissionsKey : @[ @"email", @"read_stream", @"user_groups", @"user_videos" ]),
     }
                                       completion:^(BOOL granted, NSError *error) { ...

您需要查看 Facebook 文档以了解您需要向用户请求哪些权限并填写ACFacebookPermissionsKey.

于 2013-06-24T07:13:29.760 回答
0
//Try This

-(void)fetchuserinfo
{
    NSString *accessToken = [FBSession activeSession].accessToken;
    NSLog(@"%@",accessToken);
}

or

-(void)fetchuserinfo
{
    NSLog(@"%@",FBSession.activeSession.accessToken);
}
于 2013-06-24T07:16:16.463 回答