2017-08-13 5 views
0

私はyoutube APIを使って私のウェブサイトから私のyoutubeアカウントにビデオをアップロードしました。 YouTube APIは、いつでもアカウントにAPIにアクセスするように求めています。youtube apiで私のアカウントにapiにアクセスするように頼んでいます

一つだけの時間を尋ねると、アカウントデータを保存し、次回のアップロードのために再度質問しないためにどのような方法は、これは私のコードで、私を助けてください

あります:

$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'); 
    } 

答えて

1

あなたはおそらくあなたのOAuth 2.0 credentialsを設定する必要があります。

ここからはCreate your project and select API servicesになります。

  1. オープンCredentials page
  2. APIはAPIキーとOAuth 2.0資格情報をサポートしています。プロジェクトに適しているいずれかの資格情報を作成します。

    • のOAuth 2.0を:あなたのアプリケーションは、プライベートユーザーデータにアクセスする要求でのOAuth 2.0トークンを送信する必要があります。アプリケーションは のクライアントIDと場合によってはクライアントシークレットを送信してトークンを取得します。 ウェブアプリケーション、サービスアカウント、 、またはインストールされているアプリケーション用のOAuth 2.0認証情報を生成できます。

      については、Creating OAuth 2.0 credentialsセクションを参照してください。

    • APIキー:OAuth 2.0トークンを提供しないリクエストは、APIキーを送信する必要があります。キーはプロジェクトを識別し、API のアクセス、クォータ、およびレポートを提供します。

      APIキーを作成する方法については、Creating API Keysセクションを参照してください。

+0

私はこれを設定し、私はビデオをアップロードすることができますが、私の問題は、ユーチューブのアカウントにAPIにアクセスするたびに尋ねることです。私はyoutubeに一度尋ねて、次のアップロードのためにアクセスデータを保存します。 – vm30

関連する問題