使用谷歌API获取谷歌帐户图像

时间:2015-07-08 06:47:50

标签: php google-api-php-client

第一次,我正试图通过谷歌api获取我的谷歌帐户个人资料图片。所以我提到了Refer这个网站。

所以在我的google-plus-access.php中:

if (*expr*) return;  

>

在我的index.php中:

   <?php

    require_once 'google-api-php-client-master/src/Google/autoload.php'; // or wherever autoload.php is located

    session_start();
    $client = new Google_Client();
    $client->setApplicationName("Google+ PHP Starter Application");

    $client->setClientId('MY_CLIENT_ID');
    $client->setClientSecret('MY_SECRET_KEY');
    $client->setRedirectUri('http://localhost/G-api');
    $client->setDeveloperKey('MY_DEV_KEY');

    $client->setScopes(array('https://www.googleapis.com/auth/plus.me'));
    //$plus = new apiPlusService($client);
    $plus = $client -> createAuthUrl();
    if(isset($_REQUEST['logout']))
    {
        unset($_SESSION['access_token']);
    }

    if(isset($_GET['code']))
    {
        echo $_GET['code'];
        $client->authenticate();
        $_SESSION['access_token'] = $client->getAccessToken();
        header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
    }

    if(isset($_SESSION['access_token']))
    {
        echo $_SESSION['access_token'];
        $client->setAccessToken($_SESSION['access_token']);
    }

    if ($client->getAccessToken()) {
      $me = $plus->people->get('me');
      echo $me;
      $optParams = array('maxResults' => 100);
      $activities = $plus->activities->listActivities('me', 'public', $optParams);

      $_SESSION['access_token'] = $client->getAccessToken();
    } else {
      $authUrl = $client->createAuthUrl();
      echo '<pre>';
      print_r($authUrl); // this block is running!!!
      echo '</pre>';
    }
    echo "</br>";

但我的结果是:

  <?php
     include('google-plus-access.php');
  ?>
   <img src="<?php  echo(substr($me['image']['url'],0,stripos($me['image']['url'],'?sz='))); ?>?sz=200" />

在我的控制台中,我收到以下错误: enter image description here

我的输出scree看起来像这样:

enter image description here

编辑:

在我的控制台中,我得到 https://accounts.google.com/o/oauth2/auth?response_type=code&redirect_uri=http%3A%2F%2Flocalhost%2FG-api&client_id=887633147241-8ragvhdi97lga3n829qogpl5aoima7l5.apps.googleusercontent.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fplus.me&access_type=online&approval_prompt=auto 这个。

这些事情是在本地完成的。所以有些机构让我害怕我们不能做到这一点是本地的,我们已经为此创建了域名。所以请帮助我这样做。

1 个答案:

答案 0 :(得分:0)

检查您的代码$me是否在如果 // this block is running!!!中创建   在其他中。

您正在尝试使用尚未创建的 $ me

if ($client->getAccessToken()) {
      $me = $plus->people->get('me');   //Daimto: This doesn't run so you cant use it
      echo $me;
      $optParams = array('maxResults' => 100);
      $activities = $plus->activities->listActivities('me', 'public', $optParams);

      $_SESSION['access_token'] = $client->getAccessToken();
} else {
      $authUrl = $client->createAuthUrl();
      echo '<pre>';
      print_r($authUrl); // this block is running!!!
      echo '</pre>';
}

您还需要创建服务,$plus->people->get('me');无效,因为$ plus不是Google_Service_Plus。您应该查看此处user-sample.php

中的示例
$googlePlus = new Google_Service_Plus($client);
$userProfile = $googlePlus->people->get('me');