Google Plus api for iOS get friend list
Asked Answered
T

6

2

I want to get friends(People) list from Google+ in iOS app.

I am using Google+ api tutorial given in link https://developers.google.com/+/mobile/ios/getting-started

I am created new project on Google+ Developer Console the link is

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

Getting following error in -(void)getPeopleInfo.

[lvl=3] __31-[ViewController getPeopleInfo]_block_invoke() Error: Error Domain=com.google.GTLJSONRPCErrorDomain Code=401 "The operation couldn’t be completed. (Invalid Credentials)" UserInfo=0x14d89340 {error=Invalid Credentials, 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] __31-[ViewController getPeopleInfo]_block_invoke() Error: Error Domain=com.google.GTLJSONRPCErrorDomain Code=401 "The operation couldn’t be completed. (Invalid Credentials)" UserInfo=0x14d85f90 {error=Invalid Credentials, GTLStructuredError=GTLErrorObject 0x14d85ba0: {message:"Invalid Credentials" code:401 data:[1]}, NSLocalizedFailureReason=(Invalid Credentials)}

I written the following code in 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);
                }
            }];
}
Tibbs answered 13/3, 2014 at 7:31 Comment(1)
Don't post photos of your application secrets publically, you should reset them ASAP in the Google Developers Console ASAP.Wyne
S
3

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

                }
            }];

}
Sheet answered 26/5, 2014 at 6:43 Comment(10)
i tried your code suggestion but getting this error - [lvl=3] __41-[ViewController finishedWithAuth:error:]_block_invoke() Error: Error Domain=com.google.GTLJSONRPCErrorDomain Code=401 "The operation couldn’t be completed. (Invalid Credentials)" UserInfo=0x7b05aba0 {error=Invalid Credentials, GTLStructuredError=GTLErrorObject 0x7afdfba0: {message:"Invalid Credentials" code:401 data:[1]}, NSLocalizedFailureReason=(Invalid Credentials)} . COuld u tell me where i m making mistake..Certain
@Dilip, i think check your secret key and credentials whose are provided by google developers and check the settings in google developers page. I hope this helps you. All the best!!Sheet
HI, I think you are talking with @Imran :).Aldarcie
Dilip sorry, instead of @Imran i mentioned your's nameSheet
@Imran pls check, what you inserted bundle id for your app in google account, and api key accessing in your appSheet
@Anilkumar i checked bro same bundle ide i used in console and appCertain
@Imran without seeing your code, its very tough to tell the answer, anyway have you followed these steps, developers.google.com/+/quickstart/iosSheet
@Anilkumar ya i followed same steps, able to log in successfully and fetch our deatils but friends deatils name email are not coming. i tried ur code onlyCertain
its worked for me and by the way i worked google sdk long back. May be any changes occurred in developers of google, check @ImranSheet
Let us continue this discussion in chat.Certain
S
2

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.

Shiism answered 14/3, 2014 at 6:53 Comment(0)
B
0

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.

Bernini answered 21/8, 2014 at 10:44 Comment(0)
C
0

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.

Certain answered 19/1, 2015 at 7:45 Comment(0)
H
0

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"]; 
Hargett answered 3/5, 2017 at 6:11 Comment(0)
C
-1
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);
                }


            }
        }];
Cavalryman answered 27/4, 2015 at 12:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.