【问题标题】:Accessing Picasa Web API using PHP使用 PHP 访问 Picasa Web API
【发布时间】:2015-08-24 16:26:33
【问题描述】:

既然 Google 已经开始使用 OAuth2,这里有人知道如何访问 Google Photos API 吗?他们开发者网站中的 PHP 客户端库现已过时且无法正常工作!

我已使用 OAuth 与 Google 云端硬盘一起使用,但照片无法使用! :(

首先我使用Google_Client 成功验证用户。然后在重定向页面中我正在尝试以下操作:

require_once("Google/Client.php");

//set up path for Zend GData, because Google Documentation uses that lib
$clientLibraryPath = '/path/to/ZendGData/library';
$oldPath = set_include_path(get_include_path() . PATH_SEPARATOR . $clientLibraryPath);

require_once 'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Gdata_Photos');

try
{
    $authCode = $_GET['code']; //authorization code returned from google

    //next create google OAuth Client object and validate...
    $webAuth= new Google_Client();
    $webAuth->setClientId($clientId);
    $webAuth->setClientSecret($clientSecret);
    $webAuth->authenticate($authCode); //this authenticate() works fine...

    //now my problem is HOW do I tie this to GData API for Picasa :(
    //I tried following but it throws error 
    //*Token invalid - Invalid token: Request token used when not allowed.*

    $client = Zend_Gdata_AuthSub::getHttpClient($authCode);     
    $gp = new Zend_Gdata_Photos($client, "GData:2.0");
    $userFeed = $gp->getUserFeed("default");

我还尝试了一堆第三方库,尝试将我的 $webAuth 连接到 Zend_GData_Photos

谁能帮帮我?我束手无策....我不敢相信当 Google 将身份验证更新为 OAuth 时,他们会留下一个功能齐全的库(PicasaWeb PHP API Ver 1.0)。

【问题讨论】:

    标签: php api oauth gdata google-data-api


    【解决方案1】:

    我遇到了同样的问题,但最后我让它再次工作。 最好的是,您不需要任何客户端库即可访问私人照片。

    我花了两天时间试图让它与“服务帐户”一起工作,但没有成功。

    然后我找到了这个页面: https://holtstrom.com/michael/blog/post/522/Google-OAuth2-with-PicasaWeb.html

    这帮助我实现了我想要的。 这是一篇相当长的文章,但不应该花很长时间来整理它并让它发挥作用。基本上,您需要在https://console.developers.google.com 的项目中使用“OAuth 2.0 客户端 ID”而不是“服务帐户” 在您的“OAuth 2.0 客户端 ID”中,您将获得以下信息:

    您将在验证过程中使用这些数据。 在开始之前,您需要完成 OAuth 同意屏幕。

    在该教程中,有一条说明将这些令牌存储在 DB 中,但在这种情况下,我宁愿建议直接在网页中显示它们。这要容易得多。 有人建议使用 https 而不是 http,但它应该适用于两者。 我在我的应用程序中使用了 https。

    这是上面链接中文章的较短版本。

    1. 创建 oauth2.php 文件并将其放在https://www.yoursite.com/oauth2.php

      <?php
      if (isset($_GET['code']))
      {
      $clientId = 'your-client-id.apps.googleusercontent.com';
      $clientSecret = 'your-client-secret';
      $referer = 'https://www.yoursite.com/oauth2.php';
      
      $postBody = 'code='.urlencode($_GET['code'])
                .'&grant_type=authorization_code'
                .'&redirect_uri='.urlencode($referer)
                .'&client_id='.urlencode($clientId)
                .'&client_secret='.urlencode($clientSecret);
      
      $curl = curl_init();
      curl_setopt_array( $curl,
                       array( CURLOPT_CUSTOMREQUEST => 'POST'
                             , CURLOPT_URL => 'https://accounts.google.com/o/oauth2/token'
                             , CURLOPT_HTTPHEADER => array( 'Content-Type: application/x-www-form-urlencoded'
                                                           , 'Content-Length: '.strlen($postBody)
                                                           , 'User-Agent: www.yoursite.com/0.1 +https://www.yoursite.com/'
                                                           )
                             , CURLOPT_POSTFIELDS => $postBody                              
                             , CURLOPT_REFERER => $referer
                             , CURLOPT_RETURNTRANSFER => 1 // means output will be a return value from curl_exec() instead of simply echoed
                             , CURLOPT_TIMEOUT => 15 // max seconds to wait
                             , CURLOPT_FOLLOWLOCATION => 0 // don't follow any Location headers, use only the CURLOPT_URL, this is for security
                             , CURLOPT_FAILONERROR => 0 // do not fail verbosely fi the http_code is an error, this is for security
                             , CURLOPT_SSL_VERIFYPEER => 1 // do verify the SSL of CURLOPT_URL, this is for security
                             , CURLOPT_VERBOSE => 0 // don't output verbosely to stderr, this is for security
                       ) );
      $response = curl_exec($curl);
      $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
      curl_close($curl);  
      echo($response);
      echo($http_code);
      }
      else { echo 'Code was not provided.'; }
      ?>
      
    2. 准备并访问此链接: https://accounts.google.com/o/oauth2/auth?scope=https://picasaweb.google.com/data/&response_type=code&access_type=offline&redirect_uri=https://www.yoursite.com/oauth2.php&approval_prompt=force&client_id=your-client-id.googleusercontent.com

    要调整的字段:redirect_uri 和 client_id

    1. 访问第 2 步中的链接后,您应该会看到您必须批准的同意屏幕,您将被重定向到您的 oauth.php 页面,但这次使用 code 参数:李>

    https://www.yoursite.com/oauth2.php?code=some-random-code

    1. 'code' 参数将由 oauth.php 发送到:https://accounts.google.com/o/oauth2/token

    它将返回(打印)json 格式的数据,其中包含:access_token、token_type、expires_in 和 refresh_token。

    Http 响应码应该是 200。 Access_token 将用于获取女贞专辑数据。

    1. 用内容创建index.php

      <?php
      $curl = curl_init();
      $url = 'https://picasaweb.google.com/data/entry/api/user/default';
      curl_setopt_array( $curl, 
                       array( CURLOPT_CUSTOMREQUEST => 'GET'
                             , CURLOPT_URL => $url
                             , CURLOPT_HTTPHEADER => array( 'GData-Version: 2'
                                                           , 'Authorization: Bearer '.'your-access-token' )
      
                             , CURLOPT_RETURNTRANSFER => 1 // means output will be a return value from curl_exec() instead of simply echoed
                       ) );
      $response = curl_exec($curl);
      $http_code = curl_getinfo($curl,CURLINFO_HTTP_CODE);
      curl_close($curl);
      
      echo($response . '<br/>');
      echo($http_code);
      ?>
      
    2. 从第 5 步运行脚本后,您应该会收到来自 picasaweb API 的默认供稿。当我说“默认”时,表示您登录时使用私人相册的默认值。从现在开始,您应该可以使用该方法访问您的 picasa 照片库。

    3. 访问令牌将在 3600 秒(1 小时)后过期,因此您必须获得新的令牌。这可以通过下面这样的脚本来实现:

      $clientId = 'your-client-id.apps.googleusercontent.com';
      $clientSecret = 'your-client-secret';
      $referer = 'https://www.yoursite.com/oauth2.php';
      $refreshToken = 'your-refresh-token';
      
      $postBody = 'client_id='.urlencode($clientId)
                    .'&client_secret='.urlencode($clientSecret)
                    .'&refresh_token='.urlencode($refreshToken)
                    .'&grant_type=refresh_token';
      
          $curl = curl_init();
          curl_setopt_array( $curl,
                           array( CURLOPT_CUSTOMREQUEST => 'POST'
                                 , CURLOPT_URL => 'https://www.googleapis.com/oauth2/v3/token'
                                 , CURLOPT_HTTPHEADER => array( 'Content-Type: application/x-www-form-urlencoded'
                                                               , 'Content-Length: '.strlen($postBody)
                                                               , 'User-Agent: www.yoursite.com/0.1 +https://www.yoursite.com/'
                                                               )
                                 , CURLOPT_POSTFIELDS => $postBody                              
                                 , CURLOPT_RETURNTRANSFER => 1 // means output will be a return value from curl_exec() instead of simply echoed
                                 , CURLOPT_TIMEOUT => 15 // max seconds to wait
                                 , CURLOPT_FOLLOWLOCATION => 0 // don't follow any Location headers, use only the CURLOPT_URL, this is for security
                                 , CURLOPT_FAILONERROR => 0 // do not fail verbosely fi the http_code is an error, this is for security
                                 , CURLOPT_SSL_VERIFYPEER => 1 // do verify the SSL of CURLOPT_URL, this is for security
                                 , CURLOPT_VERBOSE => 0 // don't output verbosely to stderr, this is for security
                           ) );
          $response = curl_exec($curl);
          $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
          curl_close($curl);  
      
          if (strlen($response) < 1)
          { echo('fail 01'); }
      
          $NOW = time();
          $responseDecoded = json_decode($response, true); // convert returned objects into associative arrays
          $expires = $NOW - 60 + intval($responseDecoded['expires_in']);
          if ( empty($responseDecoded['access_token'])
            || $expires <= $NOW )
          { echo('fail 02'); }
          echo($http_code . '<br/>');
          echo($response . '<br/>');
          echo($expires . '<br/>');
      ?>
      
    4. 1234563使用第 4 步中的 refresh_token 调用。
    5. 呜呜。那就是。我希望你能启动并运行它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-10
      • 2018-02-17
      • 2016-04-23
      • 1970-01-01
      相关资源
      最近更新 更多