PHP 如何上传到 Youtube 用户的 channel

标签 php youtube youtube-api

需要用户授权,以便服务器将视频上传到用户 channel 。

认证部分:

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

身份验证似乎有效并节省了一些 key 。
第二部分,尝试上传视频:
$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?

最佳答案

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.

关于PHP 如何上传到 Youtube 用户的 channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44820502/

相关文章:

youtube - YouTube Upload Widget/OnStateChange事件监听器

android - 如何编译 Youtube WatchMe?

php - 我不明白为什么我的变量为NULL

python - youtube-dl提取的视频描述不包含换行符,并且已被截断

c# - 从YouTube播放列表中获取视频ID

youtube-api - 如何只查找可以在移动设备上播放的视频?

php - 使用 MySQL 关系进行 Elastic Search 地理距离查询

php - PhP 脚本 login_tools 出错

php - 在azure网站上存储文件但不打开php

python - 属性错误: 'function' object has no attribute 'read'