YouTube数据PHP API-服务错误“凭据无效”

时间:2019-01-16 08:03:46

标签: php oauth-2.0 youtube youtube-data-api

我想通过php在YouTube上上传视频。为此,我已经按照this教程获取权限(进行身份验证),并按照this上传了视频。
身份验证可以正常工作。用户被重定向到Google网站,接受我有权上传YouTube视频等,然后再次重定向到我的网站。在那里,访问令牌已保存。
但是,如果我要上传视频,则会出现此错误:

  

发生服务错误:{“错误”:{“错误”:[{“域”:“全局”,“原因”:“ authError”,“消息”:“无效凭据”,“ locationType”:“ header”,“ location”:“ Authorization”},“ code”:401,“ message”:“ Invalid Credentials”}}

为了测试我的应用程序,我尝试retrieve my uploads而不是上传视频,但是发生了相同的错误。
我不知道是什么触发了此错误。

这是我从网站上将用户重定向到google的代码:

$OAUTH2_CLIENT_ID = "Not on Stackoverflow (but here is a code)";
$OAUTH2_CLIENT_SECRET = "Also not on Stackoverflow";

$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->addScope("https://www.googleapis.com/auth/youtube.upload");
$client->addScope("https://www.googleapis.com/auth/youtube");
$client->setRedirectUri("my site(actual link)");
$client->setAccessType('offline');        // offline access
$client->setIncludeGrantedScopes(true);   // incremental auth

$auth_url = $client->createAuthUrl();
echo '<a href="' . htmlspecialchars($auth_url) . '">Allow posting on YouTube (click)</a>';

这是我从用户重定向到Google的网站上的代码:

$OAUTH2_CLIENT_ID = "Not on Stackoverflow (but here is a code)";
$OAUTH2_CLIENT_SECRET = "Also not on Stackoverflow";

$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->addScope("https://www.googleapis.com/auth/youtube.upload");
$client->addScope("https://www.googleapis.com/auth/youtube");
$client->setRedirectUri("my site (actual link)");
$client->setAccessType('offline');        // offline access
$client->setIncludeGrantedScopes(true);   // incremental auth

$client->authenticate($_GET['code']);
$access_token = $client->getAccessToken();
//Acces token is being saved

这是上传代码(无效):

$OAUTH2_CLIENT_ID = "--";
$OAUTH2_CLIENT_SECRET = "--";

$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->addScope("https://www.googleapis.com/auth/youtube.upload");
$client->addScope("https://www.googleapis.com/auth/youtube");
$client->setRedirectUri("https://www.socialme.online/YoutubeFT.php");
$client->setAccessType('offline');        // offline access
$client->setIncludeGrantedScopes(true);   // incremental auth

$client->setAccessToken($access_token);
$youtube = new Google_Service_YouTube($client);



try {
    $snippet = new Google_Service_YouTube_VideoSnippet();
    $snippet->setTitle($Titel);
    $snippet->setDescription($Beschreibung);
    $snippet->setTags(array("tag1", "tag2"));
    $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 = 1048576;
    $client->setDefer(true);
    $insertRequest = $youtube->videos->insert("status,snippet", $video);

    $media = new Google_Http_MediaFileUpload(
        $client,
        $insertRequest,
        'video/'.$extension,
        null,
        true,
        $chunkSizeBytes
    );
    $media->setFileSize(filesize($new_path));
    // Read the media file and upload it chunk by chunk.
    $status = false;
    $handle = fopen($new_path, "rb");
    while (!$status && !feof($handle)) {
        $chunk = fread($handle, $chunkSizeBytes);
        $status = $media->nextChunk($chunk);
    }

    fclose($handle);
    $client->setDefer(false);
} catch (Google_Service_Exception $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()));
}

echo $htmlBody;

我不知道是什么原因导致此错误,尤其是因为大多数代码均来自Google的官方文档。 redirect_uri中的网址有效,并且与Google控制台中授权的网址相同。视频的链接也有效。
谢谢您的帮助。

0 个答案:

没有答案