Skip to content

Commit becd887

Browse files
"Added sample: php/add_subscription.php"
1 parent 8a1a9e4 commit becd887

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

php/add_subscription.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
// Call set_include_path() as needed to point to your client library.
4+
require_once 'Google/Client.php';
5+
require_once 'Google/Service/YouTube.php';
6+
session_start();
7+
8+
/*
9+
* You can acquire an OAuth 2.0 client ID and client secret from the
10+
* {{ Google Cloud Console }} <{{ https://cloud.google.com/console }}>
11+
* For more information about using OAuth 2.0 to access Google APIs, please see:
12+
* <https://developers.google.com/youtube/v3/guides/authentication>
13+
* Please ensure that you have enabled the YouTube Data API for your project.
14+
*/
15+
$OAUTH2_CLIENT_ID = 'REPLACE_ME';
16+
$OAUTH2_CLIENT_SECRET = 'REPLACE_ME';
17+
18+
$client = new Google_Client();
19+
$client->setClientId($OAUTH2_CLIENT_ID);
20+
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
21+
$client->setScopes('https://www.googleapis.com/auth/youtube');
22+
$redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
23+
FILTER_SANITIZE_URL);
24+
$client->setRedirectUri($redirect);
25+
26+
// Define an object that will be used to make all API requests.
27+
$youtube = new Google_Service_YouTube($client);
28+
29+
if (isset($_GET['code'])) {
30+
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
31+
die('The session state did not match.');
32+
}
33+
34+
$client->authenticate($_GET['code']);
35+
$_SESSION['token'] = $client->getAccessToken();
36+
header('Location: ' . $redirect);
37+
}
38+
39+
if (isset($_SESSION['token'])) {
40+
$client->setAccessToken($_SESSION['token']);
41+
}
42+
43+
// Check to ensure that the access token was successfully acquired.
44+
if ($client->getAccessToken()) {
45+
try {
46+
// This code subscribes the authenticated user to the specified channel.
47+
48+
// Identify the resource being subscribed to by specifying its channel ID
49+
// and kind.
50+
$resourceId = new Google_Service_YouTube_ResourceId();
51+
$resourceId->setChannelId('UCtVd0c0tGXuTSbU5d8cSBUg');
52+
$resourceId->setKind('youtube#channel');
53+
54+
// Create a snippet object and set its resource ID.
55+
$subscriptionSnippet = new Google_Service_YouTube_SubscriptionSnippet();
56+
$subscriptionSnippet->setResourceId($resourceId);
57+
58+
// Create a subscription request that contains the snippet object.
59+
$subscription = new Google_Service_YouTube_Subscription();
60+
$subscription->setSnippet($subscriptionSnippet);
61+
62+
// Execute the request and return an object containing information
63+
// about the new subscription.
64+
$subscriptionResponse = $youtube->subscriptions->insert('id,snippet',
65+
$subscription, array());
66+
67+
$htmlBody .= "<h3>Subscription</h3><ul>";
68+
$htmlBody .= sprintf('<li>%s (%s)</li>',
69+
$subscriptionResponse['snippet']['title'],
70+
$subscriptionResponse['id']);
71+
$htmlBody .= '</ul>';
72+
73+
} catch (Google_ServiceException $e) {
74+
$htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',
75+
htmlspecialchars($e->getMessage()));
76+
} catch (Google_Exception $e) {
77+
$htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',
78+
htmlspecialchars($e->getMessage()));
79+
}
80+
81+
$_SESSION['token'] = $client->getAccessToken();
82+
} else {
83+
// If the user has not authorized the application, start the OAuth 2.0 flow.
84+
$state = mt_rand();
85+
$client->setState($state);
86+
$_SESSION['state'] = $state;
87+
88+
$authUrl = $client->createAuthUrl();
89+
$htmlBody = <<<END
90+
<h3>Authorization Required</h3>
91+
<p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p>
92+
END;
93+
}
94+
?>
95+
96+
<!doctype html>
97+
<html>
98+
<head>
99+
<title>Returned Subscription</title>
100+
</head>
101+
<body>
102+
<?=$htmlBody?>
103+
</body>
104+
</html>

0 commit comments

Comments
 (0)