2013-08-22 53 views
1

我想將Google日曆界面構建到我的Web應用程序中,並使用日曆作爲事件存儲,因爲這也提供其他服務。我知道我需要獲取授權令牌,然後獲取應用程序的刷新令牌才能使用該API。我已經在Google上設置了應用程序,並提供了PHP代碼來獲取令牌。如何在Laravel中將Google日曆與API集成?

但是我真的很努力去理解我在做什麼,代碼在做什麼,並且一步一步沒有找到關於這個主題的白癡指南......我可能犯了基本的錯誤,但是不知道他們是什麼。

我發現這個職位:Google Calendar API v3 hardcoded credentials,我試圖遵循。

我跑的第一個腳本:

<?php 
$scope   = 'https://www.google.com/calendar/feeds/'; 
$client_id  = 'my id'; 
$redirect_uri = 'my redirect url'; 

$params = array(
       'response_type' => 'code', 
       'client_id'  => $client_id, 
       'redirect_uri' => $redirect_uri, 
       'scope'   => $scope 
       ); 
$url = 'https://accounts.google.com/o/oauth2/auth?' . http_build_query($params);   
echo $url."\n"; 

問題:我從谷歌API複製的重定向URI。這包括一些代碼和另一行http://localhost。我是複製兩行還是隻複製一行?無論如何,這應該爲我的應用程序定製。這個參數是做什麼的? -

我正在本地機器上運行,服務器名稱爲downsadmin.loc。我的意圖是最終在一個建成的現場服務器上運行。

我跑了由谷歌API提供的URI的腳本,它想出了一個錯誤:

Invalid parameter value for redirect_uri: Missing authority: urn:ietf:wg:oauth:2.0:oob http://localhost

所以我想我的重定向URI是錯的?

我剛剛下載了Google PHP庫。我跑了這裏的例子:

$client->setClientId('my id'); 
$client->setClientSecret('my key_'); 
$client->setRedirectUri('uri 
'); 
$client->setDeveloperKey('my developer key'); 
$plus = new Google_PlusService($client); 

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 ($client->getAccessToken()) { 
$activities = $plus->activities->listActivities('me', 'public'); 
print 'Your Activities: <pre>' . print_r($activities, true) . '</pre>'; 

// We're not done yet. Remember to update the cached access token. 
// Remember to replace $_SESSION with a real database or memcached. 
$_SESSION['token'] = $client->getAccessToken(); 
} else { 
$authUrl = $client->createAuthUrl(); 
print "<a href='$authUrl'>Connect Me!</a>"; 
} 

同樣的錯誤:

invalid parameter value for redirect_uri: Missing authority: urn:ietf:wg:oauth:2.0:oob http://downsadmin.loc/google/test_googleplus.php

任何幫助和指針讚賞 - 尤其是一個很基本的鏈接到一步步的做什麼步驟,爲什麼 - 然後下一個步驟如何開始使用API​​。

回答

1

更多的研究在絕望中,我發現的OAuth 2,本教程中,我已經在步驟2中,接着這一點,並設法讓我刷新令牌!

http://cornempire.net/2012/01/08/part-2-oauth2-and-configuring-your-application-with-google/

我最初有一個空白令牌返回,下面由用戶「北」在後添加到腳本按照註釋需要:

要解決空$ cRefreshToken問題你需要通過增加

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

curl_exec($ch);

權上啓用SSL所以現在與日合作e API