vm30
vm30

Reputation: 9

youtube api asks me to access api to my account every one

I used youtube API to upload videos from my website to my youtube account. YouTube API asks every time to access API to my account.

Is there any way to ask just one time, and save the account data, and don't ask again for next uploads, please help me

this is my code :

$client = new \Google_Client();
$OAUTH2_CLIENT_ID = 'xxxxxxxxx';
$OAUTH2_CLIENT_SECRET = 'xxxxxxxx';

$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setAuthConfig('xxxxxxxx/'.client_secrets.json');
$client->setScopes('https://www.googleapis.com/auth/youtube');
$client->setAccessType("offline");
$client->setIncludeGrantedScopes(true);
$client->setRedirectUri('http://myWebsiteUrl');

if (!isset($_GET['code'])) {
   $auth_url = $client->createAuthUrl();
   header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
   exit;
} else{
        $title = 'title';
        $descriptions = 'description';
    }
    $client->authenticate($_GET['code']);
    if (isset($_SESSION['access_token'])) {
       $client->setAccessToken($_SESSION['access_token']);
    } else {
       $_SESSION['access_token'] = $client->getAccessToken();
    }
    try {
      $youtube = new \Google_Service_YouTube($client);
      $videoPath = 'video path';

      $snippet = new \Google_Service_YouTube_VideoSnippet();
      $snippet->setTitle($title);
      $snippet->setDescription($descriptions);
      $snippet->setCategoryId('22');

      $status = new \Google_Service_YouTube_VideoStatus();
      $status->privacyStatus = "public";

      $video = new \Google_Service_YouTube_Video();
      $video->setSnippet($snippet);
      $video->setStatus($status);

      $chunkSizeBytes = 1 * 1024 * 1024;

      $client->setDefer(true);

      $insertRequest = $youtube->videos->insert("status,snippet", $video);
      $media = new \Google_Http_MediaFileUpload($client, $insertRequest, 'video/*', null, true, $chunkSizeBytes);
      $media->setFileSize(filesize($videoPath));

      $status = false;
      $handle = fopen($videoPath, "rb");
      while (!$status && !feof($handle)) {
            $chunk = fread($handle, $chunkSizeBytes);
            $status = $media->nextChunk($chunk);
      }

      fclose($handle);
      $client->setDefer(false);

   } catch (Google_Service_Exception $e) {
     echo $e->getMessage();
   } catch (Google_Exception $e) {
    die('error');
   }

Upvotes: 0

Views: 81

Answers (1)

MαπμQμαπkγVπ.0
MαπμQμαπkγVπ.0

Reputation: 6737

You maybe need to set up your OAuth 2.0 credentials.

From here, you will be able to Create your project and select API services.

  1. Open the Credentials page.
  2. The API supports API keys and OAuth 2.0 credentials. Create whichever credentials are appropriate for your project:

    • OAuth 2.0: Your application must send an OAuth 2.0 token with any request that accesses private user data. Your application sends a client ID and, possibly, a client secret to obtain a token. You can generate OAuth 2.0 credentials for web applications, service accounts, or installed applications.

      See the Creating OAuth 2.0 credentials section for more information.

    • API keys: A request that does not provide an OAuth 2.0 token must send an API key. The key identifies your project and provides API access, quota, and reports.

      See the Creating API Keys section for information about creating an API key.

Upvotes: 1

Related Questions