PHP如何上传到Youtube用户';s频道

PHP如何上传到Youtube用户';s频道,php,youtube,youtube-api,Php,Youtube,Youtube Api,需要使用户的授权,以便服务器将上传视频到用户的频道 认证部分: $client = new Google_Client(); $client->setAuthConfigFile(PROPPATH.'/inc/client_secret.json'); $client->setRedirectUri('https://back url'); $client->setScopes('https://www.googleapis.com/auth/youtube'); $clien

需要使用户的授权,以便服务器将上传视频到用户的频道

认证部分:

$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);
}
我得到了一个错误:

出现服务错误:{“错误”:{“错误”:[{“域”: “全局”,“原因”:“authError”,“消息”:“无效凭据”, “位置类型”:“标题”,“位置”:“授权”}],“代码”: 401,“消息”:“无效凭据”} 我错过了什么

401:无效凭据

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

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


我刚刚获得它并尝试使用,有什么建议吗?也许我需要使用不同的作用域来授予对用户通道的访问权限?我认为您应该检查如何获得它,确保身份验证服务器实际上正在刷新您的访问令牌。您正在使用的已过期。我已经在代码上设置了脱机访问,还有其他建议吗?
$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": "global",
        "reason": "authError",
        "message": "Invalid Credentials",
        "locationType": "header",
        "location": "Authorization",
      }
    ],
    "code": 401,
    "message": "Invalid Credentials"
  }
}