Google Plus api for iOS get friend list

人走茶凉 提交于 2019-12-04 19:13:09
Anilkumar iOS ReactNative

Call the following method, after login success, for me, i am getting friends list using below method

-(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);

                }
            }];

}

As per my experience, Google+ SDK is not having any method fetching the friend list currently.

It's suggested to use Google Contacts API for fetching contacts. It may happen that contacts fetched from this API are not active on Google+. So it's mixed list.

So, Wait for the updates from Google.

We have a way to get google plus visible friends information. Please take a look on description and if it will be not clear enough for you, then I will provide more description.

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);
});

Plaease let me know if it is not clear enough for you, then i will provide some more description.

Hi I was also facing same error but it got resolved. The problem is in setting scope.

set the scope as #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];
}

It will fetch friends details like name, image url but not Email address. For fetching Email address try to use Contact API. iOS has NSXMLParser, the contact api code is given in JS, java, net u could use that and fetch the details.

Hi i was also facing the same problem. This problem Occurring Because of scope. In Your code You have override the scope.

signIn.scopes = @[ kGTLAuthScopePlusLogin];  

With

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

So, You have to change Your scope By Simple

signIn.scopes = @[ kGTLAuthScopePlusLogin]; 

Or

signIn.scopes = @[ kGTLAuthScopePlusLogin,@"profile"]; 
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);
                }


            }
        }];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!