使用PHP的Google URL缩短API

问题描述:

现在我知道这是一个通过互联网特别是*的常见话题。但是我没有的(或者还没有看到的)对于在会话中维护的令牌是直接的解决方案。使用PHP的Google URL缩短API

我正在使用Google APIs Client Library for PHP

我的查询: 我有使用Google_Oauth2Service从PHP客户端的lib一个的index.php其中I取用户数据(如姓名)。用户认证成功,一切顺利。现在我想使用网址缩短服务,因此我有一个shorten.php,我有代码尝试获取短网址。一些如何这不起作用。

的index.php

//include google api files 
require_once 'src/Google_Client.php'; 
require_once 'src/contrib/Google_Oauth2Service.php'; 
require_once 'src/contrib/Google_UrlshortenerService.php'; 

//start session 
session_start(); 
$gClient = new Google_Client(); 
$gClient->setApplicationName('Test App'); 
$gClient->setClientId($google_client_id); 
$gClient->setClientSecret($google_client_secret); 
$gClient->setRedirectUri($google_redirect_url); 
$gClient->setDeveloperKey($google_developer_key); 

$google_oauthV2 = new Google_Oauth2Service($gClient); 
.... 
.... 

在这里,我已经开始了会议,并取得了Google_Client的对象。我已经宣布客户ID,秘密和所有其他细节。

然后,我在成功认证时获取访问令牌并将其存储在Session变量中,以便当我尝试从shorten.php中获取短url(使用jQuery ajax)时,可以使用现有的令牌。

$_SESSION['token'] = $gClient->getAccessToken();  
.... 

现在shorten.php

session_start(); 
require_once 'src/Google_Client.php'; 
require_once 'src/contrib/Google_Oauth2Service.php'; 
require_once 'src/contrib/Google_UrlshortenerService.php'; 

$gClient = new Google_Client(); 
$gClient->setApplicationName('Test App'); 
$gClient->setClientId($google_client_id); 
$gClient->setClientSecret($google_client_secret); 
$gClient->setRedirectUri($google_redirect_url); 
$gClient->setDeveloperKey($google_developer_key); 

.... 
if (isset($_SESSION['token']) && $_SESSION['token']) { 

    // Set the access token from the session 
    $gClient->setAccessToken($_SESSION['token']); 
    $url_service = new Google_UrlshortenerService($gClient); 

    // Check if a URL has been passed 
    if (isset($_GET['url'])) { 
      $url = new Google_Url(); 
     $url->longUrl = $_GET['url']; 

     $shortURL = $url_service->url->insert($url);  
      .... 

这是代码打破$shortURL = $url_service->url->insert($url);我成功地让使用Session变量的令牌,并创建了一个成功的URL服务对象的确切行。但就在我调用插入方法的时候,那是失败的时候。

Apache的错误日志

[email protected]:~/myprojects/web$ tail -1 /var/log/apache2/error.log | sed -e 's/\\n/\n/g' 
[Thu Mar 28 00:42:35 2013] [error] [client 127.0.0.1] PHP Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error calling POST https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyCxfXP-xS-QYJw-7mM4SNG3EW9ryj_Oiv4: (401) Invalid Credentials' in /home/husain/myprojects/web/apps/src/io/Google_REST.php:66 
Stack trace: 
#0 /home/husain/myprojects/web/apps/src/io/Google_REST.php(36): Google_REST::decodeHttpResponse(Object(Google_HttpRequest)) 
#1 /home/husain/myprojects/web/apps/src/service/Google_ServiceResource.php(177): Google_REST::execute(Object(Google_HttpRequest)) 
#2 /home/husain/myprojects/web/apps/src/contrib/Google_UrlshortenerService.php(38): Google_ServiceResource->__call('insert', Array) 
#3 /home/husain/myprojects/web/apps/shorten.php(44): Google_UrlServiceResource->insert(Object(Google_Url)) 
#4 {main} 
    thrown in /home/husain/myprojects/web/apps/src/io/Google_REST.php on line 66 

当我倒在的index.php和shorten.php文件Google_Client变量,这是我得到的。

的index.php

Google_Client Object 
    (
    [scopes:protected] => Array 

     (
     ) 

    [useObjects:protected] => 
    [services:protected] => Array 
     (
      [oauth2] => Array 
       (
        [scope] => Array 
         (
          [0] => https://www.googleapis.com/auth/userinfo.profile 
          [1] => https://www.googleapis.com/auth/userinfo.email 
         ) 

       ) 

     ) 

    [authenticated:Google_Client:private] => 
) 

shorten.php

object(Google_Client)#1 (4) { 
    ["scopes":protected]=> 
    array(0) { 
    } 
    ["useObjects":protected]=> 
    bool(false) 
    ["services":protected]=> 
    array(1) { 
    ["urlshortener"]=> 
    array(1) { 
     ["scope"]=> 
     string(44) "https://www.googleapis.com/auth/urlshortener" 
    } 
    } 
    ["authenticated":"Google_Client":private]=> 
    bool(false) 
} 

两者是不是一样的,所以我假设有一些东西不就在这里。请帮助或指导。

您在进行身份验证时未请求UrlShortener范围,这就是失败的原因。 若要范围增加,你可以在index.php文件验证之前创建的UrlshortenerService,通过客户端的同一个实例,如:

//start session 
session_start(); 
$gClient = new Google_Client(); 
$gClient->setApplicationName('Test App'); 
$gClient->setClientId($google_client_id); 
$gClient->setClientSecret($google_client_secret); 
$gClient->setRedirectUri($google_redirect_url); 
$gClient->setDeveloperKey($google_developer_key); 

$google_oauthV2 = new Google_Oauth2Service($gClient); 
$url_service = new Google_UrlshortenerService($gClient); 

或者,您可以使用setScopes覆盖自动生成的范围