我想查询有关使用的是iOS 6的新整合Facebook API用户的信息。 这是我使用的代码,这基本上是相同的,他们演示了在WWDC什么:
{
NSDictionary *parameters = @{};
NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/me"];
SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook
requestMethod:SLRequestMethodGET
URL:url
parameters:parameters];
request.account = self.account;
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSLog(@"Facebook request received, status code %d", urlResponse.statusCode);
NSString *response = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Response data: %@", response);
dispatch_async(dispatch_get_main_queue(), ^{
});
}];
}
问题是我从Facebook收到错误代码2500:“工作中的接入令牌必须用于查询当前用户的信息。” 如果我更改查询到https://graph.facebook.com/[facebook ID]然后正常工作。 我假设的问题是,iOS版的节点,经requestForServiceType的请求时,通过该应用程序的访问令牌,而不是用户的访问令牌。 我只是不知道如何解决它。 显然,预测和我的硬编码用户的Facebook的标识是不是一种选择。 有什么建议?
添加您的积极的访问令牌在paramete像
NSDictionary *parameters = [NSDictionary dictionaryWithObject:@"PUT_ACCESS_TOKEN_HERE" forKey:@"access_token"];
我面临着同样的问题,找到了解决方法:
NSString *uid = [NSString stringWithFormat:@"%@", [[self.account valueForKey:@"properties"] valueForKey:@"uid"]] ;
NSURL *url = [NSURL URLWithString:[@"https://graph.facebook.com" stringByAppendingPathComponent:uid]];
确定这里是我做这种整合与iOS 6,并得到了我从Facebook希望:
在AppDelegate中我这样做:
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return [FBSession.activeSession handleOpenURL:url];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[FBSession.activeSession handleDidBecomeActive];
}
- (void)applicationWillTerminate:(UIApplication *)application
{
[FBSession.activeSession close];
}
在我的ViewController,我想找回我自己或我的朋友信息,我这样做(注:这是一个测试,所以这是一个很大的权限):
NSArray *permissions =
[NSArray arrayWithObjects:@"email", @"user_location",
@"user_birthday",
@"user_likes", @"user_interests",@"friends_interests",@"friends_birthday",@"friends_location",@"friends_hometown",@"friends_photos",@"friends_status",
@"friends_about_me", @"friends_birthday", @"friends_hometown", @"friends_interests", @"friends_likes", @"friends_location", nil];
[FBSession openActiveSessionWithReadPermissions:permissions
allowLoginUI:YES
completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
/* handle success + failure in block */
if (status) {
NSLog(@"Facebook Read Permission is successful!");
[self presentPostOptions];
// [self presentPostOptions];
}
}];
然后在“presentPostOptions”我这样做(在这个例子中,我试图找回我的朋友的东西):
- (void)presentPostOptions
{
[[FBRequest requestForMyFriends] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error)
{
if (!error) {
NSArray *data = [user objectForKey:@"data"];
NSLog(@"%d", [data count]);
for (FBGraphObject<FBGraphUser> *friend in data) {
NSLog(@"%@", [friend first_name]);
NSLog(@"%@", [friend last_name]);
NSLog(@"%@", [friend id]);
//make sure you have FBProfilePictureView outlet in your view
//otherwise skip the profile picture!
self.fbProfilePic.profileID = @"you'r friend's profile.id";
}
}
else
{
NSLog(@"error");
// [self didFailWithError:error];
}
}];
我不知道你想做的事,因为你的问题,你只是试图建立连接还有什么,但这样一来,你可以做你想做什么都当你是集成在iOS 6中。
还有一两件事,要确保你的应用程序设置上的Facebook和CONFIGS那边想使你的iOS应用,并为iPhone / iPad上的ID。 另外,FacebookAppID在你的plist。
让我知道,如果它为你,
编辑:顺便说一句我的Facebook SDK 3.1.1是。
我有同样的错误信息,我通过更新凭证后,救了我的帐户固定它。
确保您的(ACAccount *)facebookAccount(或你的情况self.account)对象是强大的,你正确设置它,而获得权限。
答案很简单
在viewDidLoad中()使用方法:
accountStore= [[ACAccountStore alloc]init];
facebookAccountType= [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
NSDictionary *options= @{
ACFacebookAudienceKey: ACFacebookAudienceEveryone,
ACFacebookAppIdKey: @"<YOUR FACEBOOK APP ID>",
ACFacebookPermissionsKey: @[@"public_profile"]
};
[accountStore requestAccessToAccountsWithType:facebookAccountType options:options completion:^(BOOL granted, NSError *error) {
if (granted) {
NSLog(@"Permission has been granted to the app");
NSArray *accounts= [accountStore accountsWithAccountType:facebookAccountType];
facebookAccount= [accounts firstObject];
[self performSelectorOnMainThread:@selector(facebookProfile) withObject:nil waitUntilDone:NO];
} else {
NSLog(@"Permission denied to the app");
}
}];
而功能 - (无效)facebookProfile
NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/me"];
请注意,你需要添加为字典参阅以下完整列表PARAMS https://developers.facebook.com/docs/graph-api/reference/user
NSDictionary *param=[NSDictionary dictionaryWithObjectsAndKeys:@"picture,id,name",@"fields", nil];
SLRequest *profileInfoRequest= [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodGET URL:url parameters:param];
profileInfoRequest.account= facebookAccount;
[profileInfoRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
NSLog(@"Facebook status code is : %ld", (long)[urlResponse statusCode]);
if ([urlResponse statusCode]==200) {
NSDictionary *dictionaryData= [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableLeaves error:&error];
} else {
}
}];
}