如何获取Google Plus帐户的个人资料照片?

时间:2014-02-02 22:58:16

标签: google-plus user-profile socialauth

我正在建立一个网站,我想允许谷歌登录。我不希望我的客户再次将他们的个人资料图片上传到我的网站。我有一些关于如何使用Facebook进行操作的线索,但是如果用户通过Google帐户进行身份验证后如何从Google Plus帐户中获取个人资料图片? 这有什么API吗?如果是的话,请分享。

8 个答案:

答案 0 :(得分:8)

我们可以轻松获得加上客户端 当连接加号客户端时,即

if(plusClient.isConnected){

            plusClient.getCurrentPerson().getImage().getUrl();

}

它将为您提供图像的网址。

答案 1 :(得分:6)

您可以使用给定的技巧获取Google个人资料图片网址:

Old Trick

https://plus.google.com/s2/photos/profile/(user_id)?sz=150

但现在这不起作用,因为谷歌已经改变了他们的政策,因此下面给出了这样做的新技巧

新技巧

请求网址

https://www.googleapis.com/plus/v1/people/115950284...320?fields=image&key={YOUR_API_KEY}

这将以json格式提供Google个人资料图片网址,如下所示

回应:

{
    "image": 
    {
         "url": "https://lh3.googleusercontent.com/-OkM...AANA/ltpH4BFZ2as/photo.jpg?sz=50"
    }
}


有关详细信息,您还可以查看此信息 How to get user image through user id in Google plus?

快乐编码!!

答案 2 :(得分:2)

有关详细信息,请参阅https://developers.google.com/+/api/latest/people/get,但通常您需要执行以下步骤:

  1. 使用OAuth2,使用plus.login范围对其进行身份验证。
  2. 一旦登录,您可以使用people.get方法,其userId值为“me”。这给出了他们完整的个人资料
  3. 他们的照片将在image.url字段

答案 3 :(得分:1)

+1 @Prisoner所说的,您应该使用plus.login对用户进行身份验证,然后使用特殊字符串me检索用户。

获取照片网址和封面照片网址的完整示例。

<html>
  <head>
  <script src="https://apis.google.com/js/client:plusone.js"></script>
  </head>
  <body>
    <span id="signinButton">
      <span
        class="g-signin"
        data-callback="signinCallback"
        data-clientid="YOUR_CLIENT_ID"
        data-cookiepolicy="single_host_origin"
        data-requestvisibleactions="http://schemas.google.com/AddActivity"
        data-scope="https://www.googleapis.com/auth/plus.login">
      </span>
    </span>
    <script>
      function signinCallback (resp) {
        gapi.client.load('plus', 'v1', function() {
          gapi.client.plus.people.get({userId: 'me'}).execute(getProfilePic);
        });
      }
      function getProfilePic(person){
        console.log(person.image.url);
        console.log(person.cover.coverPhoto.url);
      }
    </script>
  </body>
</html>

当页面加载时,登录按钮呈现,当用户登录时,将触发回调。触发回调时,将加载加号客户端。加载加载客户端后,将对people.get进行API调用。

答案 4 :(得分:1)

此代码可以帮助您通过图片

获取Google+个人资料信息
<?php  
  /* * Copyright 2011 Google Inc. 
  * 
  * Licensed under the Apache License, Version 2.0 (the "License"); 
  * you may not use this file except in compliance with the License. 
  * You may obtain a copy of the License at 
  * 
  * http://www.apache.org/licenses/LICENSE-2.0 
  * 
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  * See the License for the specific language governing permissions and 
  * limitations under the License. 
  */  
  require_once 'google-api-php-client/src/apiClient.php';  
  require_once 'google-api-php-client/src/contrib/apiPlusService.php';  
session_start();  
$id = $_POST['id'];  
$client = new apiClient();  
  $client->setApplicationName("Google+ PHP Starter Application");  
  // oauth2_client_id, oauth2_client_secret, and to register your oauth2_redirect_uri.  
  $client->setClientId('357142505911.apps.googleusercontent.com');  
  $client->setClientSecret('LbJa7YOJ1Th-e-TOosEJxI4A');  
  $client->setRedirectUri('http://www.w3resource.com/API/google-plus/example.php');  
  $client->setDeveloperKey('AIzaSyD3stLpkt7jJw0mkMsJtM9m_zrgg26rrsI');  
  $plus = new apiPlusService($client);  
if (isset($_REQUEST['logout'])) {  
  unset($_SESSION['access_token']);  
  }  
if (isset($_GET['code'])) {  
  $client->authenticate();  
  $_SESSION['access_token'] = $client->getAccessToken();  
  header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);  
  }  
if (isset($_SESSION['access_token'])) {  
  $client->setAccessToken($_SESSION['access_token']);  
  }  
if ($client->getAccessToken()) {  
  $me = $plus->people->get($id);  
$optParams = array('maxResults' => 100);  
  $activities = $plus->activities->listActivities($id, 'public', $optParams);  
// The access token may have been updated lazily.  
  $_SESSION['access_token'] = $client->getAccessToken();  
  } else {  
  $authUrl = $client->createAuthUrl();  
  }  
  ?>  
<!doctype html>  
  <html>  
  <head><link rel='stylesheet' href='style.css' /></head>  
  <body>  
  <header><h1>Google+ Sample App</h1></header>  
  <div class="box">  
<?php if(isset($me) && isset($activities)): ?>  
  <div class="me">  
  <p><a rel="me" href="<?php echo $me['url'] ?>"><?php print $me['displayName'] ?></a></p>  
  <p><?php print $me['tagline'] ?></p>   
  <p><?php print $me['aboutMe'] ?></p>   
  <div><img src="<?php echo $me['image']['url']; ?>?sz=82" /></div>  
  </div>  
  <div class="activities">Your Activities:  
  <?php foreach($activities['items'] as $activity): ?>  
  <div class="activity">  
  <p><a href="<?php print $activity['url'] ?>"><?php print $activity['title'] ?></a></p>  
  </div>  
  <?php endforeach ?>  
  </div>  
  <?php endif ?>  
  <?php  
  if(isset($authUrl)) {  
  print "<a class='login' href='$authUrl'>Connect Me!</a>";  
  } else {  
  //print "<a class='logout' href='?logout'>Logout</a>";  
  }  
  ?>  
  </div>  
  </body>  
  </html>  

来源:http://www.w3resource.com/API/google-plus/tutorial.php#

答案 5 :(得分:0)

根据新的Google登录方式的文档,It

  

public Uri getPhotoUrl()

     
    

如果用户有个人资料照片并且您从新的GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)}或配置了requestProfile()开始构建配置,则返回已登录用户的照片网址。否则为null。即使配置,也不保证所有用户都能出现。

  

如最后一行所说Not guaranteed to be present for all users, even when configured,你应该处理null case。

P / S:有时,如果已经创建了Google+个人资料图片,但是在您的设备中添加Google帐户之后,您可能需要从设备中删除现有的Google帐户,然后重新添加。

答案 6 :(得分:0)

http://www.avatarapi.com/提供了一个API,根据Google的公开信息返回用户的姓名和个人资料照片。

可以在此API端点通过SOAP或HTTP调用它: http://www.avatarapi.com/avatar.asmx

此API的一个好处是它不需要用户通过Google进行身份验证。但是,在您的特定情况下,这将是一个问题。

答案 7 :(得分:-2)

建议使用以下网址here

https://plus.google.com/s2/photos/profile/GOOGLE-ID?sz=100

我以前从未在任何文档中看到过这种情况,因此可能不受Google+支持,但现在正在运作。

也许+ class或+囚犯可以说一下这个网址的支持来获取个人资料照片?