반응형
Google API PHP 클라이언트 라이브러리 설치
composer require google/apiclient:^2.0
예제 코드
<?php
// Load the Google API PHP Client Library.
require_once __DIR__ . '/vendor/autoload.php';
// Define an object that will be used to make all API requests.
$client = new Google_Client();
$client->setApplicationName('YouTube Data API PHP Quickstart');
$client->setScopes('https://www.googleapis.com/auth/youtube.force-ssl');
$client->setAuthConfig('client_secret.json');
$client->setAccessType('offline');
// Define a function that will be executed when authentication is required.
function getAuthorizationCode($client) {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open this link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);
// Check to see if there was an error.
if (array_key_exists('error', $accessToken)) {
throw new Exception(join(', ', $accessToken));
}
}
// Check if an auth token exists for the required scopes
$tokenPath = 'token.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->setAccessToken($accessToken);
}
// If there is no previous token or it's expired.
if ($client->isAccessTokenExpired()) {
// Refresh the token if possible, else fetch a new one.
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
} else {
getAuthorizationCode($client);
}
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}
// Define service object for making API requests.
$service = new Google_Service_YouTube($client);
// Define a resource id for subscribing to a channel.
$resourceId = new Google_Service_YouTube_ResourceId();
$resourceId -> setChannelId("Channel ID"); // channel id
// Create a subscription snippet with resource id.
$subscriptionSnippet = new Google_Service_YouTube_SubscriptionSnippet();
$subscriptionSnippet -> setResourceId($resourceId);
// Create a subscription request with snippet.
$subscriptionRequest = new Google_Service_YouTube_Subscription();
$subscriptionRequest -> setSnippet($subscriptionSnippet);
try {
// Execute subscription request and print response.
$response = $service -> subscriptions -> insert("snippet", $subscriptionRequest);
print_r("Successfully subscribed to FreeCodeCamp channel!");
} catch (Google_Service_Exception $e) {
print_r(sprintf('<p>A service error occurred: <code>%s</code></p>',
htmlspecialchars($e->getMessage())));
} catch (Google_Exception $e) {
print_r(sprintf('<p>An client error occurred: <code>%s</code></p>',
htmlspecialchars($e->getMessage())));
}
?>
반응형
'DEVEL > PHP' 카테고리의 다른 글
PHP Coinbase API를 사용해서 비트코인(BTC) 전송 (0) | 2023.07.06 |
---|---|
PHP Google 인증 토큰 유효성 체크 (0) | 2023.04.11 |
PHP CURL POST 요청하기 (0) | 2023.03.15 |
PHP 유튜브 동영상 정보 가져오기 (0) | 2023.03.15 |
PHP 유튜브 시청 기록 가져오기 (0) | 2023.03.15 |