适用于iOS的Google Plus api获取好友列表 [英] Google Plus api for iOS get friend list

查看:118
本文介绍了适用于iOS的Google Plus api获取好友列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在iOS应用中从Google+获取朋友(人物)列表。

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

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

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

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

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数据:[1]},NSLocalizedFailureReason =(无效凭据s)}
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 =(无效凭证)}

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

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


推荐答案

之后调用以下方法登录成功,对我来说,我正在使用以下方法获取好友列表

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

                }
            }];

}

这篇关于适用于iOS的Google Plus api获取好友列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆