ios - 如何从 FBSession 获取访问 token ?
<p><p>我正在使用 Facebook SDK 来获取应用程序中的用户组
但我得到一个空访问 token 。
我在 <code>viewDidLoad</code> 中的代码如下。</p>
<pre><code>- (void)viewDidLoad
{
;
self.session = [ initWithAppID:appID permissions:@[@"basic_info",@"user_groups",@"email"] defaultAudience:FBSessionDefaultAudienceEveryone urlSchemeSuffix:nil tokenCacheStrategy:nil];
allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
NSLog(@"Error in Session Opening %@",error.description);
NSLog(@"access Token %@",session.accessTokenData.accessToken);
[FBRequestConnection startWithGraphPath:@"me/groups" parameters:nil HTTPMethod:@"GET" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSLog(@"%@",result);
}else
{
NSLog(@"Error %@",error.description);
}
}];
}];
}
</code></pre>
<p><strong>这段代码的输出是:-</strong></p>
<pre><code>Error in Session Opening (null)
access Token (null)
Error Error Domain=com.facebook.sdk Code=5 "The operation couldn’t be completed. (com.facebook.sdk error 5.)" UserInfo=0x1090acc80 {com.facebook.sdk:HTTPStatusCode=400, com.facebook.sdk:ParsedJSONResponseKey={
body = {
error = {
code = 2500;
message = "An active access token must be used to query information about the current user.";
type = OAuthException;
};
};
code = 400;
}}
</code></pre>
<p>如您所见,访问 token 为空..</p>
<p>我做错了什么?</p>
<p>在这段代码中是否应该添加一些额外的东西?</p>
<p>谢谢</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>你想要这个:</p>
<pre><code>session.accessTokenData.accessToken
</code></pre>
<p>'session' 是你的 FBSession</p>
<p>你可以从中得到...</p>
<pre><code>if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded)
{
// If there's one, just open the session silently, without showing the user the login UI
allowLoginUI:NO
completionHandler:^(FBSession *session, FBSessionState state, NSError *error)
{
NSLog(@"%@", session.accessTokenData.accessToken);
}];
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何从 FBSession 获取访问 token ?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/21440673/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/21440673/
</a>
</p>
页:
[1]