2013-03-27 99 views
0

現在我知道這是一個通過互聯網特別是StackOverflow的常見話題。但是我沒有的(或者還沒有看到的)對於在會話中維護的令牌是直接的解決方案。使用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) 
} 

兩者是不是一樣的,所以我假設有一些東西不就在這裏。請幫助或指導。

回答

0

您在進行身份驗證時未請求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覆蓋自動生成的範圍