Google Plus API for iOS获取好友列表

我想在iOS应用中获取来自Google+的朋友(人)列表。

我正在使用https://developers.google.com/+/mobile/ios/getting-started中提供的Google+ api教程

我在Google+开发人员控制台上创build了一个新项目

https://console.developers.google.com/project

– (void)getPeopleInfo中获得跟随错误

错误:错误域= com.google.GTLJSONRPCErrorDomain代码= 401“该操作无法完成。(无效的凭据)”UserInfo = 0x14d89340 {错误=无效的凭据, GTLStructuredError = GTLErrorObject 0x14d855e0:{message:“Invalid Credentials”code:401 data:[1]},NSLocalizedFailureReason =(Invalid Credentials)} 2014-03-13 12:40:21.026 GPlusDemo [636 / 0x3d35718c] [lvl = 3] (无效凭证)“UserInfo = 0x14d85f90 {错误=无效凭证,GTLStructuredError = GTLErrorObject 0x14d85ba0:错误:错误域= com.google.GTLJSONRPCErrorDomain代码= 401”操作无法完成。 {message:“Invalid Credentials”code:401 data:[1]},NSLocalizedFailureReason =(Invalid Credentials)}

我在ViewController.m中编写了下面的代码

- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. GPPSignIn *signIn = [GPPSignIn sharedInstance]; signIn.shouldFetchGooglePlusUser = YES; //signIn.shouldFetchGoogleUserEmail = YES; // Uncomment to get the user's email // You previously set kClientId in the "Initialize the Google+ client" step signIn.clientID = kClientId; // Uncomment one of these two statements for the scope you chose in the previous step signIn.scopes = @[ kGTLAuthScopePlusLogin]; // "https://www.googleapis.com/auth/plus.login" scope signIn.scopes = @[ @"profile" ]; // "profile" scope // Optional: declare signIn.actions, see "app activities" signIn.delegate = self; [signIn trySilentAuthentication]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)refreshInterfaceBasedOnSignIn { if ([[GPPSignIn sharedInstance] authentication]) { // The user is signed in. NSLog(@"Login"); self.signInButton.hidden = YES; // Perform other actions here, such as showing a sign-out button [self getPeopleInfo]; } else { self.signInButton.hidden = NO; // Perform other actions here } } - (void)finishedWithAuth: (GTMOAuth2Authentication *)auth error: (NSError *) error { NSLog(@"Received error %@ and auth object %@",error, auth); if (error) { // Do some error handling here. } else { [self refreshInterfaceBasedOnSignIn]; } } - (void)signOut { [[GPPSignIn sharedInstance] signOut]; } - (void)disconnect { [[GPPSignIn sharedInstance] disconnect]; } - (void)didDisconnectWithError:(NSError *)error { if (error) { NSLog(@"Received error %@", error); } else { // The user is signed out and disconnected. // Clean up user data as specified by the Google+ terms. } } -(void)getPeopleInfo { GTLServicePlus* plusService = [[GTLServicePlus alloc] init]; plusService.retryEnabled = YES; [plusService setAuthorizer:[GPPSignIn sharedInstance].authentication]; GTLQueryPlus *query = [GTLQueryPlus queryForPeopleListWithUserId:@"me" collection:kGTLPlusCollectionVisible]; [plusService executeQuery:query completionHandler:^(GTLServiceTicket *ticket, GTLPlusPeopleFeed *peopleFeed, NSError *error) { if (error) { GTMLoggerError(@"Error: %@", error); } else { // Get an array of people from GTLPlusPeopleFeed NSArray* peopleList = [peopleFeed.items mutableCopy]; NSLog(@"peopleList:%@", peopleList); } }]; } 

调用以下方法,login成功后,对于我来说,我正在使用下面的方法获取好友列表

 -(void)finishedWithAuth: (GTMOAuth2Authentication *)auth error: (NSError *) error { GTLServicePlus* plusService = [[GTLServicePlus alloc] init]; plusService.retryEnabled = YES; [plusService setAuthorizer:[GPPSignIn sharedInstance].authentication]; GTLQueryPlus *query = [GTLQueryPlus queryForPeopleListWithUserId:@"me" collection:kGTLPlusCollectionVisible]; [plusService executeQuery:query completionHandler:^(GTLServiceTicket *ticket, GTLPlusPeopleFeed *peopleFeed, NSError *error) { if (error) { GTMLoggerError(@"Error: %@", error); } else { // Get an array of people from GTLPlusPeopleFeed NSArray* peopleList = peopleFeed.items; NSLog(@"peopleList %@ ",peopleList); } }]; } 

根据我的经验,Google+ SDK目前没有任何获取好友列表的方法。

build议使用Google Contacts API来获取联系人。 从此API获取的联系人在Google+上可能无法激活。 所以这是混合列表。

所以,请等待Google的更新。

我们有一个方法来获取谷歌加可见的朋友信息。 请看看描述,如果不够清楚,那么我会提供更多描述。

 GTMOAuth2Authentication *auth; /*That you will get when you login by your google plus account. So I am considering that you already have it.*/ NSMutableArray *arrFriends = [NSMutableArray new]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^ { NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://www.googleapis.com/plus/v1/people/%@/people/visible?orderBy=alphabetical&access_token=%@",@"your_user_id",auth.accessToken]]; /*When you login via Google plus and fetch your profile information, you will get your user id.*/ ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url]; [request startSynchronous]; if(LOGS_ON) NSLog(@"GooglePlusConnect-->getchGooglePlusFriends-->responseString = %@",request.responseString); }); 

如果你不清楚,请告诉我,然后我会提供一些更多的描述。

嗨,我也面临同样的错误,但它得到解决。 问题在于设定范围。

将范围设置为#define kGTLAuthScopePlusLogin @“ https://www.googleapis.com/auth/plus.login

 - (void)viewDidLoad { [super viewDidLoad]; GPPSignIn *signInG = [GPPSignIn sharedInstance]; signInG.shouldFetchGooglePlusUser = YES; signInG.shouldFetchGoogleUserEmail = YES; signInG.clientID = kClientId; signInG.scopes = @ [kGTLAuthScopePlusLogin]; signInG.delegate = self; [signInG trySilentAuthentication]; } 

它会获取朋友的详细信息,如名称,图像的url,而不是电子邮件地址。 要获取电子邮件地址,请尝试使用联系人API。 iOS有NSXMLParser,联系api代码是在JS,Java,networking你可以使用,并获取细节。

 GTLServicePlus* plusService = [[GTLServicePlus alloc] init]; plusService.retryEnabled = YES; [plusService setAuthorizer:[GPPSignIn sharedInstance].authentication]; GTLQueryPlus *query = [GTLQueryPlus queryForPeopleListWithUserId:@"me" collection:kGTLPlusCollectionVisible]; [plusService executeQuery:query completionHandler:^(GTLServiceTicket *ticket, GTLPlusPeopleFeed *peopleFeed, NSError *error) { if (error) { GTMLoggerError(@"Error: %@", error); } else { // Get an array of people from GTLPlusPeopleFeed NSArray* peopleList = peopleFeed.items; NSLog(@"peopleList %@ ",peopleList.description); for (NSArray *dict in peopleFeed.items) { NSString *strID=(NSString*)((GTLPlusPerson*)dict).identifier; NSLog(@"strID %@",strID); } } }]; 

嗨,我也面临同样的问题。 由于范围,发生此问题。 在你的代码中你已经覆盖了范围。

 signIn.scopes = @[ kGTLAuthScopePlusLogin]; 

 signIn.scopes = @[ @"profile" ]; // "profile" scope 

所以,你必须改变你的范围通过简单

 signIn.scopes = @[ kGTLAuthScopePlusLogin]; 

要么

 signIn.scopes = @[ kGTLAuthScopePlusLogin,@"profile"];