【问题标题】:PHP How to upload to Youtube user's channelPHP 如何上传到 Youtube 用户的频道
【发布时间】:2017-12-02 21:09:28
【问题描述】:

需要用户授权,服务器才会将视频上传到用户频道。

认证部分:

$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));
...

我得到了错误:

发生服务错误:{ "error": { "errors": [ { "domain": “全局”,“原因”:“authError”,“消息”:“无效凭据”, “位置类型”:“标题”,“位置”:“授权”}],“代码”: 401,“消息”:“无效凭据”} } 我错过了什么?

【问题讨论】:

    标签: php youtube youtube-api


    【解决方案1】:

    401:无效的凭据

    授权标头无效。您使用的访问令牌是 过期或无效。

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

    建议的操作:使用长寿命的刷新访问令牌 刷新令牌。

    【讨论】:

    • 我刚拿到它并尝试使用,有什么建议吗?
    • 也许我需要使用不同的范围来授予对用户频道的访问权限?
    • 我认为您应该检查如何获取它,确保身份验证服务器实际上正在刷新您的访问令牌。你用的那个已经过期了。 stackoverflow.com/q/9241213/1841839
    • 我已经在code上设置了离线访问,还有什么建议吗?
    猜你喜欢
    • 2016-10-19
    • 2012-01-08
    • 2013-02-10
    • 1970-01-01
    • 2015-08-01
    • 2016-04-16
    • 2017-02-12
    • 2015-06-27
    • 2017-05-06
    相关资源
    最近更新 更多