2.เข้ากูเกิล คอนโซล
https://console.developers.google.com/
3.กรณี ไม่มี โปรเจค ต้องสร้างก่อน
4.enable api ที่ต้องการ กรณีนี้เรา จะทดสอบ ไดรฟ
5.สร้าง credential เพื่อให้ เวป php เราคุยกับ server google ได้
6.เซ็ต ค่า origin และ callback เสร็จแล้ว โหลด json
7.ไปโหลด กูเกิล php library ที่เวป https://github.com/google/google-api-php-client
8.สร้างไฟล์ ทดสอบ ใน folder examples
test1.php
<?phprequire_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php');//require_once 'google-api-php-client/autoload.php';session_start();$client = new Google_Client();$client->setAuthConfigFile( 'client_secrets1.json');$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {$client->setAccessToken($_SESSION['access_token']);$drive_service = new Google_Service_Drive($client);// Print the names and IDs for up to 10 files.$optParams = array('pageSize' => 10,'fields' => "nextPageToken, files(id, name)");$files_list = $drive_service->files->listFiles($optParams);if (count($files_list->getFiles()) == 0) {print "No files found.\n";} else {print "Files:\n";foreach ($files_list->getFiles() as $file) {printf("%s (%s)\n", $file->getName(), $file->getId());}}} else {$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/gapi/examples/callback.php';header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));}?>
9.สร้างไฟล์callback
10.ลองทดสอบ เรียกจาก locahost<?phprequire_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php');session_start();$client = new Google_Client();$client->setAuthConfigFile('client_secrets1.json');$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);if (! isset($_GET['code'])) {$auth_url = $client->createAuthUrl();header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));} else {$client->authenticate($_GET['code']);$_SESSION['access_token'] = $client->getAccessToken();$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/gapi/examples/test1.php';header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));}?>
ไม่มีความคิดเห็น:
แสดงความคิดเห็น