菜鸟教程小白 发表于 2022-12-13 06:53:02

ios - Google Plus api for iOS 获取好友列表


                                            <p><p>我想在 iOS 应用中从 Google+ 获取 friend (人物)列表。</p>

<p>我正在使用链接中提供的 Google+ api 教程
<a href="https://developers.google.com/+/mobile/ios/getting-started" rel="noreferrer noopener nofollow">https://developers.google.com/+/mobile/ios/getting-started</a> </p>

<p>我在 Google+ 开发者控制台上创建了新项目,链接是 </p>

<p> <a href="https://console.developers.google.com/project" rel="noreferrer noopener nofollow">https://console.developers.google.com/project</a> </p>

<p>在 <strong>-(void)getPeopleInfo 中出现以下错误。</strong></p>

<blockquote>
<p> __31-_block_invoke() Error: Error Domain=com.google.GTLJSONRPCErrorDomain Code=401 &#34;The operation couldn’t be completed. (Invalid Credentials)&#34; UserInfo=0x14d89340 {error=Invalid Credentials, GTLStructuredError=GTLErrorObject 0x14d855e0: {message:&#34;Invalid Credentials&#34; code:401 data:}, NSLocalizedFailureReason=(Invalid Credentials)}
2014-03-13 12:40:21.026 GPlusDemo __31-_block_invoke() Error: Error Domain=com.google.GTLJSONRPCErrorDomain Code=401 &#34;The operation couldn’t be completed. (Invalid Credentials)&#34; UserInfo=0x14d85f90 {error=Invalid Credentials, GTLStructuredError=GTLErrorObject 0x14d85ba0: {message:&#34;Invalid Credentials&#34; code:401 data:}, NSLocalizedFailureReason=(Invalid Credentials)}</p>
</blockquote>

<p>我在<strong>ViewController.m</strong></p>中写了如下代码

<pre><code>- (void)viewDidLoad
{
    ;
    // Do any additional setup after loading the view, typically from a nib.


    GPPSignIn *signIn = ;
    signIn.shouldFetchGooglePlusUser = YES;
    //signIn.shouldFetchGoogleUserEmail = YES;// Uncomment to get the user&#39;s email

    // You previously set kClientId in the &#34;Initialize the Google+ client&#34; step
    signIn.clientID = kClientId;

    // Uncomment one of these two statements for the scope you chose in the previous step
    signIn.scopes = @[ kGTLAuthScopePlusLogin];// &#34;https://www.googleapis.com/auth/plus.login&#34; scope
    signIn.scopes = @[ @&#34;profile&#34; ];            // &#34;profile&#34; scope

    // Optional: declare signIn.actions, see &#34;app activities&#34;
    signIn.delegate = self;

    ;

}

- (void)didReceiveMemoryWarning
{
    ;
    // Dispose of any resources that can be recreated.
}


-(void)refreshInterfaceBasedOnSignIn
{
    if ([ authentication]) {
      // The user is signed in.
      NSLog(@&#34;Login&#34;);
      self.signInButton.hidden = YES;
      // Perform other actions here, such as showing a sign-out button

      ;
    } else {
      self.signInButton.hidden = NO;
      // Perform other actions here
    }
}

- (void)finishedWithAuth: (GTMOAuth2Authentication *)auth
                   error: (NSError *) error
{
    NSLog(@&#34;Received error %@ and auth object %@&#34;,error, auth);
    if (error) {
      // Do some error handling here.
    } else {
      ;
    }
}

- (void)signOut {
    [ signOut];
}


- (void)disconnect {
    [ disconnect];
}

- (void)didDisconnectWithError:(NSError *)error {
    if (error) {
      NSLog(@&#34;Received error %@&#34;, error);
    } else {
      // The user is signed out and disconnected.
      // Clean up user data as specified by the Google+ terms.
    }
}

-(void)getPeopleInfo
{
    GTLServicePlus* plusService = [ init];
    plusService.retryEnabled = YES;

    .authentication];


    GTLQueryPlus *query =
    [GTLQueryPlus queryForPeopleListWithUserId:@&#34;me&#34;
                                    collection:kGTLPlusCollectionVisible];
    [plusService executeQuery:query
            completionHandler:^(GTLServiceTicket *ticket,
                              GTLPlusPeopleFeed *peopleFeed,
                              NSError *error) {
                if (error) {
                  GTMLoggerError(@&#34;Error: %@&#34;, error);
                } else {
                  // Get an array of people from GTLPlusPeopleFeed
                  NSArray* peopleList = ;

                  NSLog(@&#34;peopleList:%@&#34;, peopleList);
                }
            }];
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>登录成功后调用以下方法,对我来说,我使用以下方法获取好友列表</p>

<pre><code>-(void)finishedWithAuth: (GTMOAuth2Authentication *)auth
                   error: (NSError *) error {


GTLServicePlus* plusService = [ init];

    plusService.retryEnabled = YES;

    .authentication];
    GTLQueryPlus *query =
    [GTLQueryPlus queryForPeopleListWithUserId:@&#34;me&#34;
                                    collection:kGTLPlusCollectionVisible];
    [plusService executeQuery:query
            completionHandler:^(GTLServiceTicket *ticket,
                              GTLPlusPeopleFeed *peopleFeed,
                              NSError *error) {
                if (error) {
                  GTMLoggerError(@&#34;Error: %@&#34;, error);
                } else {
                  // Get an array of people from GTLPlusPeopleFeed
                  NSArray* peopleList = peopleFeed.items;
                  NSLog(@&#34;peopleList %@ &#34;,peopleList);

                }
            }];

}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - Google Plus api for iOS 获取好友列表,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/22371931/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/22371931/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - Google Plus api for iOS 获取好友列表