2012-11-06 42 views
4

我是新來整合API和我試圖與google-api-php-client-0.6.0工作,我註冊了我在谷歌應用程序,也simple.php在配置它們dev.siva.com虛擬主機google-api-php-client-0.5.0 callbackurl?

<?php 
/* 
* Copyright 2012 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 'src/Google_Client.php'; 
session_start(); 

$client = new Google_Client(); 
$client->setApplicationName('Google Contacts PHP Sample'); 
$client->setScopes("http://www.google.com/m8/contacts/"); 
// Documentation: http://code.google.com/apis/gdata/docs/2.0/basics.html 
// Visit https://code.google.com/apis/console?api=contacts to generate your 
// oauth2_client_id, oauth2_client_secret, and register your oauth2_redirect_uri. 
// $client->setClientId('insert_your_oauth2_client_id'); 
// $client->setClientSecret('insert_your_oauth2_client_secret'); 
// $client->setRedirectUri('insert_your_redirect_uri'); 
// $client->setDeveloperKey('insert_your_developer_key'); 

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

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

if (isset($_REQUEST['logout'])) { 
    unset($_SESSION['token']); 
    $client->revokeToken(); 
} 

if ($client->getAccessToken()) { 
    $req = new Google_HttpRequest("https://www.google.com/m8/feeds/contacts/default/full"); 
    $val = $client->getIo()->authenticatedRequest($req); 

    // The contacts api only returns XML responses. 
    $response = json_encode(simplexml_load_string($val->getResponseBody())); 
    print "<pre>" . print_r(json_decode($response, true), true) . "</pre>"; 

    // The access token may have been updated lazily. 
    $_SESSION['token'] = $client->getAccessToken(); 
} else { 
    $auth = $client->createAuthUrl(); 
} 

if (isset($auth)) { 
    print "<a class=login href='$auth'>Connect Me!</a>"; 
    } else { 
    print "<a class=logout href='?logout'>Logout</a>"; 
} 

我能夠登錄並且我將重定向網址提供給了上面的代碼,我正在重定向,但問題是我無法連接頁面。爲什麼?

的URL是這樣的重定向

https://dev.siva.com/simple.php?code=4/hqJo9FDtjcn46uuP6JXE 
DuWLKTQn.Eo91flf65bgaXE-sT2ZLcbRbNNSqdQI 
+1

我以前有過未知的問題...你有最新的svn更新嗎? http://code.google.com/p/google-api-php-client/wiki/GettingStarted#Obtaining_the_most_up-to-date_version_from_SVN – Neograph734

+0

我沒有安裝任何svn –

+2

奧克,那麼你可以嘗試。有時候發佈的版本不是最好的,補丁通過SVN稍後提交。所以我建議你先安裝一個SVN客戶端。 – Neograph734

回答

相關問題