如何在 iOS 上获取 Facebook 访问令牌

发布于 2024-11-07 07:50:03 字数 483 浏览 0 评论 0原文

iOS初学者在这里。我有以下代码:

[facebook authorize:nil delegate:self];
NSString *string1=[facebook accessToken];
NSLog(string1);

日志显示: miFOG1WS_7DL88g6d95Uxmzz7GCShsWx_FHuvZkmW0E.eyJpdiI6IjNZZkFBY1c5ZnBaMGEzOWM2RzNKbEEifQ.LNjl06lsOQCO9ArVARqff3Ur2XQHku3CMHDBKkpGg351EB33LGxV v96Hh5R860KDJL0cLq8QezSW0GovYxnhUucOwxlITV364sVKDXIzC3bAn9P_74r2Axl1SgOaTyMMkQ_aSQ2OWh-8d3Zn9BDt3pXVWzBLJ9I4XAosnw0GjuE

这似乎太长,不可能是访问令牌。我读到它应该只有 40 个字符长。我做错了什么?

iOS beginner here. I have the following code:

[facebook authorize:nil delegate:self];
NSString *string1=[facebook accessToken];
NSLog(string1);

The log shows: miFOG1WS_7DL88g6d95Uxmzz7GCShsWx_FHuvZkmW0E.eyJpdiI6IjNZZkFBY1c5ZnBaMGEzOWM2RzNKbEEifQ.LNjl06lsOQCO9ArVARqff3Ur2XQHku3CMHDBKkpGg351EB33LGxVv96Hh5R860KDJL0cLq8QezSW0GovYxnhUucOwxlITV364sVKDXIzC3bAn9P_74r2Axl1SgOaTyMMkQ_aSQ2OWh-8d3Zn9BDt3pXVWzBLJ9I4XAosnw0GjuE

This seems too long to be an access token. I read it's supposed to be only 40 characters long. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

海之角 2024-11-14 07:50:03

使用截至 2013 年 3 月 20 日的 Facebook SDK v3.2.1。

NSString *fbAccessToken = [[[FBSession activeSession] accessTokenData] accessToken];

如果您更喜欢点语法,

NSString *fbAccessToken = [FBSession activeSession].accessTokenData.accessToken;

对于那些使用 Swift 的人;

var fbAccessToken = FBSession.activeSession().accessTokenData.accessToken

v4.1.0 SDK 更新

Objective-C

NSString *fbAccessToken = [FBSDKAccessToken currentAccessToken].tokenString;

Swift

var fbAccessToken = FBSDKAccessToken.currentAccessToken().tokenString

Using v3.2.1 as of March 20, 2013 of Facebook SDK.

NSString *fbAccessToken = [[[FBSession activeSession] accessTokenData] accessToken];

If you prefer dot syntax,

NSString *fbAccessToken = [FBSession activeSession].accessTokenData.accessToken;

For those using Swift;

var fbAccessToken = FBSession.activeSession().accessTokenData.accessToken

Update for v4.1.0 SDK onwards

Objective-C

NSString *fbAccessToken = [FBSDKAccessToken currentAccessToken].tokenString;

Swift

var fbAccessToken = FBSDKAccessToken.currentAccessToken().tokenString
献世佛 2024-11-14 07:50:03

如果您使用 Facebook SDK,则可以使用 FBSession 对象来获取和 accessToken。
如果用户通过应用程序身份验证并且您有活动会话,请使用:

NSString * accessToken = [[FBSession activeSession] accessToken];

获取访问令牌。

这对我有用。

If you are using the Facebook SDK you can use the FBSession object to get and accessToken.
If the user authenticated to the app and you have an active session use:

NSString * accessToken = [[FBSession activeSession] accessToken];

to get the access token.

It worked for me.

终弃我 2024-11-14 07:50:03

您可以使用委托方法:

- (void)fbDialogLogin:(NSString *)token expirationDate:(NSDate *)expirationDate 

有关更多信息,您可以参考以下问题:Facebook 访问令牌

You can use delegate method:

- (void)fbDialogLogin:(NSString *)token expirationDate:(NSDate *)expirationDate 

for more information you can refer to the following question :Facebook Access Token

淡笑忘祈一世凡恋 2024-11-14 07:50:03

对于 SDK 4.0

NSString *fbAccessToken = [[FBSDKAccessToken currentAccessToken] tokenString];

For SDK 4.0

NSString *fbAccessToken = [[FBSDKAccessToken currentAccessToken] tokenString];
南渊 2024-11-14 07:50:03

对于 Swift 1.2 和 FB SDK 4.1:

var fbAccessToken = FBSDKAccessToken.currentAccessToken().tokenString

For Swift 1.2 and FB SDK 4.1:

var fbAccessToken = FBSDKAccessToken.currentAccessToken().tokenString
无人接听 2024-11-14 07:50:03

对于 FacebookLogin (0.9.0) Swift

import FacebookLogin

AccessToken.current?.tokenString

For FacebookLogin (0.9.0) Swift

import FacebookLogin

AccessToken.current?.tokenString
缱绻入梦 2024-11-14 07:50:03
- (IBAction)facebookClick:(id)sender {

    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];

     [login logOut];

    login = [[FBSDKLoginManager alloc] init];


    login.loginBehavior = FBSDKLoginBehaviorWeb;
    [login logInWithReadPermissions:@[@"email"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
        if (error)
        {
                 NSLog(@"Process error");
             //   [login logOut];
         } else if (result.isCancelled) {
             NSLog(@"Cancelled");
            // [login logOut];
         } else {
             NSLog(@"Logged in");
             [self getUserInformation];
         }
     }];
}
- (IBAction)facebookClick:(id)sender {

    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];

     [login logOut];

    login = [[FBSDKLoginManager alloc] init];


    login.loginBehavior = FBSDKLoginBehaviorWeb;
    [login logInWithReadPermissions:@[@"email"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
        if (error)
        {
                 NSLog(@"Process error");
             //   [login logOut];
         } else if (result.isCancelled) {
             NSLog(@"Cancelled");
            // [login logOut];
         } else {
             NSLog(@"Logged in");
             [self getUserInformation];
         }
     }];
}
动听の歌 2024-11-14 07:50:03

检查下面的代码以从 ACAccountStore 获取 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 {
         //Error
         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");
         }

     }
 }];

Check the code below for getting the access_token from ACAccountStore :

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 {
         //Error
         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");
         }

     }
 }];
微暖i 2024-11-14 07:50:03

雨燕 4 (2018)

FBSDKAccessToken.current().tokenString

Swift 4 (2018)

FBSDKAccessToken.current().tokenString
篱下浅笙歌 2024-11-14 07:50:03

对于需要在 SDK 3.2.1 中使用 swift 的人:

let token = FBSession.activeSession().accessTokenData.accessToken

For people who need to use swift with SDK 3.2.1:

let token = FBSession.activeSession().accessTokenData.accessToken
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文