1

Google 캘린더 API를 PHP 라이브러리와 함께 사용하려고하며 Google API에 대한 사용자 인증 문제에 직면하고 있습니다.Google API 및 OAuth 2.0

질문이 있습니다. 나는 당신이 setDeveloperKey() 메소드를 사용하여 Google_Client 객체에 Api 키/개발자 키를 설정해야만하는 곳을 보았지만, 그렇지 않은 사람들도 보았다. 누군가가 내게 어떤 차이가 있는지 설명 할 수 있을까요?

내가하고 싶은 건 내 응용 프로그램에 Google 계정이있는 사용자를 연결하여 캘린더의 일정을 추가, 나열, 제거 등 할 수 있습니다. 이것은 인증을 위해 내가 지금하고있는 일입니다.

$client = new Google_Client(); 
$client->setApplicationName("Test GCAL"); 
$client->setClientId($clientid); 
$client->setClientSecret($clientsecret); 
$client->setRedirectUri($callback_url); 
$client->setAccessType("offline"); 
$client->setApprovalPrompt("force"); 

$client->setScopes("https://www.googleapis.com/auth/calendar"); 
$service = new Google_Service_Calendar($client); 

내가 제대로하고 있습니까?

내가 분석 할 수있는 작동 가능한 주석 코드가 있습니까? 나는 인터넷에서 작동하는 하나를 찾을 수 없습니다 .. 또는 어쩌면 구글 API 및 oauth 물건에 대한 모든 것을 설명하는 튜토리얼. 토큰에 대해 너무 혼란스럽고 아무도 리프레시 토큰을 사용하지 않는 것 같습니다. 그리고 그것은 나에게 필수적입니다.하지만 내가 틀렸어? 나는 당신을 생각하지 않는다

답변

1

귀하의 답변

감사 내가 공개 API를 사용할 수 있도록하는 유일한 사용했다고 의심하지만 난 정말 시험 약을 생각하지 않은 setDeveloperKey를 사용할 필요가 그것 전에. 나는 그걸 좀 더 들여다 볼 것입니다.

Oauth2와 (과) 연결된 Google 캘린더에 사용하는 코드입니다. Accessing Google Calendar with PHP – Oauth2에서 직접 찢어

<?php  
require_once 'Google/Client.php'; 
require_once 'Google/Service/Calendar.php'; 
require_once 'CalendarHelper.php'; 
session_start(); 
$client = new Google_Client(); 
$client->setApplicationName("Client_Library_Examples"); 
$client->setDeveloperKey("AIzaSyBBH88dIQPjcl5nIG-n1mmuQ12J7HThDBE"); 
$client->setClientId('2046123799103-i6cjd1hkjntu5bkdkjj5cdnpcu4iju8p.apps.googleusercontent.com'); 
$client->setClientSecret('6s4YOx3upyJhtwnetovfK40e'); 
$client->setRedirectUri('http://localhost/google-api-php-client-samples/Calendar/oauth2Pure.php'); 
$client->setAccessType('offline'); // Gets us our refreshtoken 

$client->setScopes(array('https://www.googleapis.com/auth/calendar.readonly')); 


//For loging out. 
if (isset($_GET['logout'])) { 
    unset($_SESSION['token']); 
} 


// Step 2: The user accepted your access now you need to exchange it. 
if (isset($_GET['code'])) { 

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

// Step 1: The user has not authenticated we give them a link to login  
if (!isset($_SESSION['token'])) { 

    $authUrl = $client->createAuthUrl(); 

    print "<a class='login' href='$authUrl'>Connect Me!</a>"; 
}  
// Step 3: We have access we can now create our service 
if (isset($_SESSION['token'])) { 
    $client->setAccessToken($_SESSION['token']); 
    print "<a class='logout' href='".$_SERVER['PHP_SELF']."?logout=1'>LogOut</a><br>"; 

    $service = new Google_Service_Calendar($client);  

    $calendarList = $service->calendarList->listCalendarList();; 
    print_r($calendarList); 
    while(true) { 
     foreach ($calendarList->getItems() as $calendarListEntry) { 
      echo $calendarListEntry->getSummary()."<br>\n"; 
     } 
     $pageToken = $calendarList->getNextPageToken(); 
     if ($pageToken) { 
      $optParams = array('pageToken' => $pageToken); 
      $calendarList = $service->calendarList->listCalendarList($optParams); 
     } else { 
      break; 
     } 
    } 
} 
?> 
+0

답장을 보내 주셔서 감사합니다. 나는 그것을 시도하고 당신에게 말할 것입니다. (비록 당신의 app 정보를 숨긴다 : 당신은 그것을 다음과 같이 보여서는 안된다 : p) 그러나 새로 고침 토큰을 사용하는 것처럼 보이지 않는다. 귀하의 리디렉션 URI가이 페이지로 연결됩니까? – Devz

+0

애플 리케이션 정보가 변경되었지만 튜토리얼을 작업 할 때 변경된 방법이 나를 쉽게 이해할 수 있습니다 :) 리디렉션 URI는 개발자 콘솔에 설정되어 있습니다. 자습서를 작업 할 때 localhost를 사용합니다. 클라이언트 lib는 모든 새로 고침 토큰을 처리합니다. 당신이 print_r 세션 토큰을 볼 수 있다고 생각합니다. – DaImTo

+1

좋습니다. 작동 중입니다. 정말 고맙습니다 ! 나는이 모든 것을 수업에 넣음으로써 약간의 변화를 가져올 것이다. 사실, 그것은 아주 간단합니다. 나는이 일 동안 아하하러왔다! – Devz