2012-12-12 31 views
0

我有一個名爲TasksLogin.php文件,它讓我登錄谷歌任務API認證不工作PHP

session_start(); 
require_once 'google-api-php-client/src/Google_Client.php'; 
require_once 'google-api-php-client/src/contrib/Google_TasksService.php'; 
$client = new Google_Client(); 
$client->setClientId('xxxxx.apps.googleusercontent.com'); 
$client->setClientSecret('xxxxxxxxx'); 
$client->setRedirectUri('http://xxxxxxxxx/Tasks/TaskVis.html'); 
$client->setApplicationName("TasksForMike"); 
$tasksService = new Google_TasksService($client); 

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

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

} else { 
    $client->setAccessToken($client->authenticate($_GET['code'])); 
    $_SESSION['token'] = $client->getAccessToken(); 

} 

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

這似乎工作,因爲它重定向到TaskVis.php 但在TasksVis.php我打電話:

$(document).ready(function() { 
    $.get("tasks.php", function(data){ 
var json = data; 
}); 
}); 

這是獲取任務並將它們打包在json對象中的php文件。 但在tasks.php,我有這樣的代碼崩潰:

session_start(); 
require_once 'google-api-php-client/src/Google_Client.php'; 
require_once 'google-api-php-client/src/contrib/Google_TasksService.php'; 
$client = new Google_Client(); 
$client->setClientId('xxxxxx.apps.googleusercontent.com'); 
$client->setClientSecret('xxxxxxxxxxxxxxx'); 
$client->setRedirectUri('http://xxxxxxxxx/Tasks/TaskVis.html'); 
$client->setApplicationName("TasksForMike"); 
$tasksService = new Google_TasksService($client); 

if (isset($_REQUEST['logout'])) 
{ 
    unset($_SESSION['token']); 
} 
if (isset($_GET['code'])) { 
    $client->authenticate($_GET['code']); 
    echo "hh"; 
    die(); 
    $_SESSION['token'] = $client->getAccessToken(); 
    header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']); 

} 
?> 

的「死」從來沒有運行,我得到一個500服務器錯誤。由於代碼位於查詢字符串中,因此$client->authenticate($_GET['code']);失敗?我試圖從渲染中分離出數據代碼。

回答

1

我不認爲在查詢字符串中有任何'代碼'。重定向到TaskVis.php時,您的「代碼」值將丟失,並且不會將其包含在重定向中。然後,在該文件中,你打電話tasks.php這裏...

$.get("tasks.php", function(data){ 

...因此,如果有任何_GET值,它們將被包括在呼叫:

$.get("tasks.php?code=xxxx", function(data){ 

我也有興趣知道500錯誤的性質。你有沒有嘗試過在Firebug中查看控制檯時調用它?