2017-06-29 5 views
0

サーバーがユーザーのチャンネルにビデオをアップロードするようにユーザーの承認を行う必要があります。PHP Youtubeユーザーのチャンネルにアップロードする方法

認証一部:

$client = new Google_Client(); 
$client->setAuthConfigFile(PROPPATH.'/inc/client_secret.json'); 
$client->setRedirectUri('https://back url'); 
$client->setScopes('https://www.googleapis.com/auth/youtube'); 
$client->setAccessType('offline'); 
$credentialsPath = PROPPATH.'/youtube_auth/user_'.get_current_user_id().'.json'; 
if (file_exists($credentialsPath)) { 
    $accessToken = file_get_contents($credentialsPath); 
} else { 

$authUrl = $client->createAuthUrl(); 
if(!isset($_GET['code'])) { 
    echo '<script>window.location = "'.$authUrl.'";</script>'; 
} else { 
    $authCode = $_GET['code']; 
    $accessToken = $client->authenticate($authCode); 
    file_put_contents($credentialsPath, $accessToken); 
} 

認証が作品のように思えるし、いくつかのキーが保存されます。アップロード動画をしようと 第二の部分、:

$client = new Google_Client(); 
$client->setAuthConfigFile(PROPPATH.'/inc/client_secret.json'); 
$client->setScopes('https://www.googleapis.com/auth/youtube'); 
$youtube = new Google_Service_YouTube($client); 
$client->setAccessToken(file_get_contents(PROPPATH.'/youtube_auth/user_2.json')); 
$client->setAccessType('offline'); 
if ($client->getAccessToken()) { 
$video = new Google_Service_YouTube_Video(); 
$chunkSizeBytes = 1 * 1024 * 1024; 
$client->setDefer(true); 
$insertRequest = $youtube->videos->insert("", $video); 
$media = new Google_Http_MediaFileUpload(
    $client, 
    $insertRequest, 
    'video/*', 
    null, 
    true, 
    $chunkSizeBytes 
); 
$media->setFileSize(filesize($videoPath)); 
... 

と私はエラーを取得しています:

A service error occurred: { "error": { "errors": [ { "domain": "global", "reason": "authError", "message": "Invalid Credentials", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Invalid Credentials" } } What i'm missing?

答えて

0

401: Invalid Credentials

Invalid authorization header. The access token you're using is either expired or invalid.

{ 
    "error": { 
    "errors": [ 
     { 
     "domain": "global", 
     "reason": "authError", 
     "message": "Invalid Credentials", 
     "locationType": "header", 
     "location": "Authorization", 
     } 
    ], 
    "code": 401, 
    "message": "Invalid Credentials" 
    } 
} 

Suggested action: Refresh the access token using the long-lived refresh token.

+0

私はちょうどそれを取得して、任意の提案を使用しようと? – user2455079

+0

多分私はユーザーのチャンネルへのアクセスを許可するために異なるスコープを使用する必要がありますか? – user2455079

+0

私はあなたがそれを取得していることを確認して、認証サーバーが実際にアクセストークンを更新していることを確認する必要があると思います。使用しているものが期限切れです。 https://stackoverflow.com/q/9241213/1841839 – DaImTo

関連する問題