适用于iOS的Google Plus api获取好友列表

时间:2014-03-13 07:31:32

标签: ios iphone google-plus google-plus-one

我想在iOS应用中通过Google+收到朋友(人物)列表。

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

我在Google+开发者控制台上创建了新项目链接

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

- (void)getPeopleInfo。

中收到以下错误
  

[lvl = 3] __31- [ViewController getPeopleInfo] _block_invoke()错误:错误Domain = com.google.GTLJSONRPCErrorDomain Code = 401“操作无法完成。(无效凭据)”UserInfo = 0x14d89340 {error =凭证无效,GTLStructuredError = GTLErrorObject 0x14d855e0:{message:“Invalid Credentials”代码:401 data:[1]},NSLocalizedFailureReason =(无效凭据)}   2014-03-13 12:40:21.026 GPlusDemo [636 / 0x3d35718c] [lvl = 3] __31- [ViewController getPeopleInfo] _block_invoke()错误:错误Domain = com.google.GTLJSONRPCErrorDomain Code = 401“操作无法进行已完成。(无效凭据)“UserInfo = 0x14d85f90 {error = Invalid Credentials,GTLStructuredError = GTLErrorObject 0x14d85ba0:{message:”Invalid Credentials“代码:401 data:[1]},NSLocalizedFailureReason =(证书无效)}

我在 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);
                }
            }];
}

6 个答案:

答案 0 :(得分:3)

登录成功后,请为以下方法调用以下方法,我使用以下方法获取好友列表

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

                }
            }];

}

答案 1 :(得分:2)

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

建议使用Google Contacts API获取联系人。从此API获取的联系人可能会在Google+上无效。所以它是混合列表。

所以,等待Google的更新。

答案 2 :(得分:0)

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

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

如果对您不够清楚,请告诉我,然后我会提供更多说明。

答案 3 :(得分:0)

您好我也面临同样的错误,但它已经解决了。问题在于设定范围。

将范围设置为#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];
}

它会获取朋友的详细信息,如姓名,图片网址,但不会收到电子邮件地址。要获取电子邮件地址,请尝试使用Contact API。 iOS有NSXMLParser,联系api代码在JS,java,net中给出,你可以使用它并获取详细信息。

答案 4 :(得分:0)

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

 <div class="container">
        <form class="form-horizontal">
            <div class="form-group">  
                <div class="col-sm-10 col-md-10 col-lg-10 col-sm-offset-2">
   <input type="email" class="form-control form-control input-lg" id="email" placeholder="Enter number">
                </div>
            </div>
            <div class="form-group">
               <div class="col-sm-10 col-md-10 col-lg-10 col-sm-offset-2">
                    <button type="submit" class="btn btn-default btn-lg btn-block">Submit</button>
                </div>
            </div>

signIn.scopes = @[ kGTLAuthScopePlusLogin];  

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

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

signIn.scopes = @[ kGTLAuthScopePlusLogin]; 

答案 5 :(得分:-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);
                }


            }
        }];