gpt4 book ai didi

iphone - Facebook在ios6中获取用户信息:An active access token must be used to query

转载 作者:可可西里 更新时间:2023-11-01 03:37:32 24 4
gpt4 key购买 nike

我正在通过 Accounts Framework 集成 facebook,我搜索并找到了一些方法来做到这一点。它是第一次工作,但后来它显示在日志下方并且没有提供任何信息。

日志:

Dictionary contains: {
error = {
code = 2500;
message = "An active access token must be used to query information about the current user.";
type = OAuthException;
};
}

我使用的代码

ACAccountStore *_accountStore=[[ACAccountStore alloc] init];;
ACAccountType *facebookAccountType = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

// We will pass this dictionary in the next method. It should contain your Facebook App ID key,
// permissions and (optionally) the ACFacebookAudienceKey
NSArray * permissions = @[@"email"];

NSDictionary *options = @{ACFacebookAppIdKey :@"my app id",
ACFacebookPermissionsKey :permissions,
ACFacebookAudienceKey:ACFacebookAudienceFriends};

// Request access to the Facebook account.
// The user will see an alert view when you perform this method.
[_accountStore requestAccessToAccountsWithType:facebookAccountType
options:options
completion:^(BOOL granted, NSError *error) {
if (granted)
{
// At this point we can assume that we have access to the Facebook account
NSArray *accounts = [_accountStore accountsWithAccountType:facebookAccountType];

// Optionally save the account
[_accountStore saveAccount:[accounts lastObject] withCompletionHandler:nil];

//NSString *uid = [NSString stringWithFormat:@"%@", [[_accountStore valueForKey:@"properties"] valueForKey:@"uid"]] ;
NSURL *requestURL = [NSURL URLWithString:[@"https://graph.facebook.com" stringByAppendingPathComponent:@"me"]];

SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:requestURL
parameters:nil];
request.account = [accounts lastObject];
[request performRequestWithHandler:^(NSData *data,
NSHTTPURLResponse *response,
NSError *error) {

if(!error){
NSDictionary *list =[NSJSONSerialization JSONObjectWithData:data
options:kNilOptions error:&error];
NSLog(@"Dictionary contains: %@", list );
userName=[list objectForKey:@"name"];
NSLog(@"username %@",userName);

userEmailID=[list objectForKey:@"email"];
NSLog(@"userEmailID %@",userEmailID);

userBirthday=[list objectForKey:@"birthday"];
NSLog(@"userBirthday %@",userBirthday);

userLocation=[[list objectForKey:@"location"] objectForKey:@"name"];
NSLog(@"userLocation %@",userLocation);
}
else{
//handle error gracefully
}

}];
}
else
{
NSLog(@"Failed to grant access\n%@", error);
}
}];

任何线索 friend 出了什么问题......谢谢。

最佳答案

问题是,当我在设备中更改我的 facebook 设置时,访问 token 超时。因此,如果您监听 ACAccountStoreDidChangeNotification,您可以调用 renewCredentialsForAccount: 来提示用户获得许可。

下面的代码正在运行并在字典中获取用户信息。

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(accountChanged) name:ACAccountStoreDidChangeNotification object:nil];


}

-(void)getUserInfo
{

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

NSString *key = @"your_app_id";
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);
[self get];
} else {
//Fail gracefully...
NSLog(@"error getting permission %@",e);

}
}];
}


-(void)accountChanged:(NSNotification *)notif//no user info associated with this notif
{
[self attemptRenewCredentials];
}


-(void)attemptRenewCredentials{
[self.accountStore renewCredentialsForAccount:(ACAccount *)self.facebookAccount completion:^(ACAccountCredentialRenewResult renewResult, NSError *error){
if(!error)
{
switch (renewResult) {
case ACAccountCredentialRenewResultRenewed:
NSLog(@"Good to go");
[self get];
break;

case ACAccountCredentialRenewResultRejected:

NSLog(@"User declined permission");

break;

case ACAccountCredentialRenewResultFailed:

NSLog(@"non-user-initiated cancel, you may attempt to retry");

break;

default:
break;

}
}

else{

//handle error gracefully

NSLog(@"error from renew credentials%@",error);

}

}];
}

-(void)get
{

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

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)
{
NSDictionary *list =[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

NSLog(@"Dictionary contains: %@", list );
}
else{
//handle error gracefully
NSLog(@"error from get%@",error);
//attempt to revalidate credentials
}

}];

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

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


[self.accountStore requestAccessToAccountsWithType:FBaccountType options:dictFB completion:
^(BOOL granted, NSError *e) {}];

}

This one helped me a lot.

关于iphone - Facebook在ios6中获取用户信息:An active access token must be used to query,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15492568/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com