【问题标题】:Google Plus api for iOS get friend listGoogle Plus api for iOS 获取好友列表
【发布时间】:2014-04-17 19:00:56
【问题描述】:

我想在 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:{消息:“无效凭据”代码:401 数据:[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" 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);
                }
            }];
}

【问题讨论】:

  • 不要公开发布您的应用程序机密照片,您应该尽快在 Google Developers Console 中重置它们。

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


【解决方案1】:

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

建议使用 Google Contacts API 来获取联系人。从该 API 获取的联系人可能在 Google+ 上不活跃。所以它是混合列表。

所以,等待 Google 的更新。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 我尝试了您的代码建议,但收到此错误 - [lvl=3] __41-[ViewController finishedWithAuth:error:]_block_invoke() 错误:错误 Domain=com.google.GTLJSONRPCErrorDomain Code=401 “The操作无法完成。(无效凭证)“UserInfo=0x7b05aba0 {error=Invalid Credentials, GTLStructuredError=GTLErrorObject 0x7afdfba0: {message:"Invalid Credentials" code:401 data:[1]}, NSLocalizedFailureReason=(Invalid Credentials)} .你能告诉我我在哪里犯错吗..
    • @Dilip,我想检查一下你的密钥和由谷歌开发者提供的凭据,并检查谷歌开发者页面中的设置。我希望这可以帮助你。万事如意!
    • 嗨,我想你正在和@Imran 交谈 :)。
    • Dilip 抱歉,我没有提到@Imran,而是提到了你的名字
    • @Imran 请检查您在 Google 帐户中为您的应用插入的捆绑包 ID,以及在您的应用中访问的 api 密钥
    【解决方案3】:

    我们有办法获取 google plus 可见的朋友信息。请看一下描述,如果对您来说不够清楚,我会提供更多描述。

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

    如果对您来说不够清楚,请告诉我,然后我将提供更多描述。

    【讨论】:

      【解决方案4】:

      您好,我也遇到了同样的错误,但已解决。问题在于设置范围。

      将范围设置为#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 中给出,你可以使用它并获取详细信息。

      【讨论】:

        【解决方案5】:
        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);
                        }
        
        
                    }
                }];
        

        【讨论】:

          【解决方案6】:

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

          signIn.scopes = @[ kGTLAuthScopePlusLogin];  
          

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

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

          signIn.scopes = @[ kGTLAuthScopePlusLogin]; 
          

          或者

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-06-04
            • 2016-03-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多