2014-12-24 4 views
2

Google 토플 URL에 코드가있어 액세스 토큰으로 변환 할 수 있습니까? 컬을 사용했지만 작동하지 않습니다. 배터와 쉬운 생각 나는 그것을 codeigniter에서 사용해야합니다. 이고 그 authorization_code는 무엇입니까?Google api + php + codeigniter + curl

<?php 

$field='code='.$_REQUEST['code'].'&client_id=clirnt-id 
&client_secret=client-secret 
&redirect_uri=http: // localost/googleapi/curlreq.php 
&grant_type=authorization_code'; 



$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL,"https://www.googleapis.com/oauth2/v3/token"); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS,$field); 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: application/x-www-form-urlencoded')); 

$server_output = curl_exec ($ch); 

echo 'end'; 

echo '<br/>'; 
//echo $server_output; 
print_r($server_output); 

curl_close ($ch); 
?> 
+0

https://github.com/google/google-api-php-client –

+0

시도해보십시오. Hybrid Auth가 제공합니다. 그것은 모든 apis yahoo, fb, g +, twitter everthing을 제공합니다 .. 정말 좋았어요. –

답변

1

먼저 액세스 토큰을 받아야합니다.

require_once 'google-api-php-client/autoload.php'; 

$client = new Google_Client(); 
$client->setApplicationName("Client_Library_Examples"); 
$client->setDeveloperKey("YOUR_PUBLIC_API_ACCESS_KEY"); 
$client->setScopes(array('https://www.googleapis.com/auth/plus.me')); 
$client->setClientId('CLIENT_ID'); 
$client->setClientSecret('CLIENT_SECRET'); 
$client->setApprovalPrompt('force'); 
$client->setAccessType('offline'); 
$client->setRedirectUri('http://yourdomain.com/oauthCallback'); 

$access_token = ''; 

if (!isset($_GET['code'])) { 

    redirect($client->createAuthUrl()); 

} else { 

    // Exchanging code with access token 
    $client->authenticate(urldecode($_GET['code'])); // Exchange code for auth 

    $access_token = $client->getAccessToken(); // Get access token 

    // Send your curl request to any api with access token here and get data 

} 

이것은 일반적인 흐름입니다. 먼저 implementing OAuth here in official documentation을 보셔야합니다.

일단 액세스 토큰을 갖게되면 모든 API에 대해 REST를 호출 할 수 있지만 그 전에는 액세스 할 기능의 범위를 지정해야합니다.