public function subscribChannel($username)
 {
     try {
         // Subscribe to a channel
         // Create a resource id with channel id and kind.
         // List Channels for the user
         $channels = $this->youtube->channels->listChannels('snippet', array("forUsername" => $username));
         if (empty($channels) || !isset($channels['items'])) {
             return "Not Subscribed!";
         }
         $channelId = $channels['items'][0]['id'];
         $resourceId = new Google_ResourceId();
         $resourceId->setChannelId($channelId);
         $resourceId->setKind('youtube#channel');
         // Create a snippet with resource id.
         $subscriptionSnippet = new Google_SubscriptionSnippet();
         $subscriptionSnippet->setResourceId($resourceId);
         // Create a subscription request with snippet.
         $subscription = new Google_Subscription();
         $subscription->setSnippet($subscriptionSnippet);
         // Execute the request and return an object containing information about the new subscription
         $subscriptionResponse = $this->youtube->subscriptions->insert('id,snippet', $subscription, array());
         $htmlBody = "<h3>Subscription</h3><ul>";
         $htmlBody .= sprintf('<li>%s (%s)</li>', $subscriptionResponse['snippet']['title'], $subscriptionResponse['id']);
         $htmlBody .= '</ul>';
     } catch (Google_ServiceException $e) {
         $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>', htmlspecialchars($e->getMessage()));
         return $htmlBody;
     } catch (Google_Exception $e) {
         $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>', htmlspecialchars($e->getMessage()));
         return $htmlBody;
     }
     $_SESSION['token'] = $this->client->getAccessToken();
     return true;
 }
Example #2
0
require_once 'config.php';
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_ResellerService.php';
require_once 'constants.php';
// Build the client using some custom configuration values.
$client = new Google_Client($GOOGLE_CLIENT_CONFIG);
$client->setApplicationName("Rapid Reseller - Transfer Token Demo (PHP)");
$client->setScopes($SETTINGS['OAUTH2_SCOPES']);
// Authenticate the client.
$client->setAssertionCredentials(new Google_AssertionCredentials($SETTINGS['OAUTH2_SERVICE_ACCOUNT_EMAIL'], $SETTINGS['OAUTH2_SCOPES'], file_get_contents($SETTINGS['OAUTH2_PRIVATE_KEY']), 'notasecret', 'http://oauth.net/grant_type/jwt/1.0/bearer', $SETTINGS['RESELLER_ADMIN']));
// This is ugly, but the client ID must be set here.
$client->setClientId($SETTINGS['OAUTH2_CLIENT_ID']);
// A service object takes a constructed and authenticated client.
$service = new Google_ResellerService($client);
// Create a new subscription object.
$subscription = new Google_Subscription();
$subscription->setCustomerId($_POST['customerDomain']);
$subscription->setSubscriptionId('123');
$subscription->setSkuId(ResellerSKU::GoogleApps);
$subscription->setPurchaseOrderId("mypurchaseorder-123");
// Subscriptions have a plan.
// Note: It is not possible to transfer a premier customer to a trial.
$plan = new Google_SubscriptionPlan();
$plan->setPlanName(ResellerPlanName::FLEXIBLE);
$plan->setIsCommitmentPlan(FALSE);
// Only annual subscription plans have a commitment interval.
// The following lines are commented out for reference.
/*
    $interval = new Google_SubscriptionPlanCommitmentInterval();
    $interval->setStartTime(time());
    $interval->setEndTime(time() + (86400 * 365));
/**
 * Subscribe to notifications for the current user.
 *
 * @param Google_MirrorService $service Authorized Mirror service.
 * @param string $collection Collection to subscribe to (supported
 *                           values are "timeline" and "locations").
 * @param string $user_token Opaque token used by the Service to
 *                          identify the  user the notification pings
 *                          are sent for (recommended).
 * @param string $callback_url URL receiving notification pings (must be HTTPS).
 */
function subscribe_to_notifications($service, $collection, $user_token, $callback_url)
{
    try {
        $subscription = new Google_Subscription();
        $subscription->setCollection($collection);
        $subscription->setUserToken($user_token);
        $subscription->setCallbackUrl($callback_url);
        $service->subscriptions->insert($subscription);
        return "Subscription inserted!";
    } catch (Exception $e) {
        return 'An error occurred: ' . $e->getMessage();
    }
}
if (isset($_SESSION['token'])) {
    $client->setAccessToken($_SESSION['token']);
}
// Check if access token successfully acquired
if ($client->getAccessToken()) {
    try {
        // Subscribe to a channel
        // Create a resource id with channel id and kind.
        $resourceId = new Google_ResourceId();
        $resourceId->setChannelId('UCtVd0c0tGXuTSbU5d8cSBUg');
        $resourceId->setKind('youtube#channel');
        // Create a snippet with resource id.
        $subscriptionSnippet = new Google_SubscriptionSnippet();
        $subscriptionSnippet->setResourceId($resourceId);
        // Create a subscription request with snippet.
        $subscription = new Google_Subscription();
        $subscription->setSnippet($subscriptionSnippet);
        // Execute the request and return an object containing information about the new subscription
        $subscriptionResponse = $youtube->subscriptions->insert('id,snippet', $subscription, array());
        $htmlBody .= "<h3>Subscription</h3><ul>";
        $htmlBody .= sprintf('<li>%s (%s)</li>', $subscriptionResponse['snippet']['title'], $subscriptionResponse['id']);
        $htmlBody .= '</ul>';
    } catch (Google_ServiceException $e) {
        $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>', htmlspecialchars($e->getMessage()));
    } catch (Google_Exception $e) {
        $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>', htmlspecialchars($e->getMessage()));
    }
    $_SESSION['token'] = $client->getAccessToken();
} else {
    // If the user hasn't authorized the app, initiate the OAuth flow
    $state = mt_rand();