请求的授权令牌是什么?(优酷API)



在我使用完整的C/Curl之前,我正在尝试遵循Google的Youtube API在PHP/Curl中的可恢复上传。这是我的代码。

<?php
    $resource           = "www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet,status,contentDetails";
    $query              = '{
      "snippet": {
        "title": "My video title",
        "description": "This is a description of my video",
        "tags": ["cool", "video", "more keywords"],
        "categoryId": 22
      },
      "status": {
        "privacyStatus": "public",
        "embeddable": True,
        "license": "youtube"
      }
    }';
    $response           = NULL;
    
    # Initialize PHP/CURL handle
    $ch                 = curl_init();
    $request_headers    = array();
    $request_headers[]  = 'Authorization: Bearer *****';
    $request_headers[]  = 'Content-Length: 278';
    $request_headers[]  = 'Content-Type: application/json; charset=UTF-8';
    $request_headers[]  = 'X-Upload-Content-Length: 3000000';
    $request_headers[]  = 'X-Upload-Content-Type: video/*';
    curl_setopt($ch, CURLOPT_HTTPHEADER     , $request_headers          );
    curl_setopt($ch, CURLOPT_POSTFIELDS     , $query                    );
    curl_setopt($ch, CURLOPT_POST           , TRUE                      );
    curl_setopt($ch, CURLOPT_HTTPGET        , FALSE                     );
    curl_setopt($ch, CURLOPT_HEADER         , FALSE                     );      // Include head as needed
    curl_setopt($ch, CURLOPT_NOBODY         , FALSE                     );      // Return body
    curl_setopt($ch, CURLOPT_URL            , $resource                 );      // Target site
    curl_setopt($ch, CURLOPT_VERBOSE        , TRUE                      );      // Minimize logs
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER , FALSE                     );      // No certificate
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION , TRUE                      );      // Follow redirects
    curl_setopt($ch, CURLOPT_RETURNTRANSFER , TRUE                      );      // Return in string
    # Create return array
    $response           = curl_exec($ch); 
    $info               = curl_getinfo($ch);
    $error              = curl_error($ch);
    # Close PHP/CURL handle
    curl_close($ch);
    return $response;
?>

每当我运行脚本时,我都会收到 403 禁止错误。我认为问题来自授权令牌。我想我没有使用我应该使用的东西。谷歌说

"请求设置以下 HTTP 请求标头:

  • 授权 – 请求的授权令牌。

我不确定它到底是什么,所以我使用了使用 Youtube PHP 客户端 API 生成的访问令牌,但仍然没有。

检查此代码。也许这可以帮助你。

在这种情况下,您可以在响应标头中获取位置参数。请参阅下面的代码和我得到的响应标头。此外,在$requestBody中使用干净清晰的JSON数据

<?php
function startResumableSession() {
    $requestBody = '{"snippet":{"title":"test video","description":"testing api","tags":["any","thing"],"categoryId":25},"status":{"privacyStatus":"public","embeddable":"true","license":"youtube"}}';
    $headers = array
    (
        "POST /upload/youtube/v3/videos?uploadType=resumable&part=snippet,status HTTP/1.1",
        "Host: www.googleapis.com",
        "Content-length: 0",
        "Content-type: application/json",
        "Authorization: Bearer xxxxxxxxxxxxxACCESS_TOKENxxxxxxxxxxxxxxxxxx",
    );
    /* Parameter values in the request URL must be URL-encoded. */
    $url = "https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet,status";
    $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
    curl_setopt($ch, CURLOPT_POSTFIELDS,urlencode($requestBody));
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_PROXY, "192.168.10.5:8080");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,TRUE);
    $result = curl_exec($ch);
    $info = curl_getinfo($ch);
    $json = json_decode($result);
    /* debug info */
    return array('response'=>(array)$result,
        'info' => $info,
        'headers'=>$headers
    );
}

这是响应的标头。在这里,您可以找到带有上传ID的位置。有关下一步,请访问此链接 保存可恢复会话 URI

    HTTP/1.1 200 Connection established
    HTTP/1.1 200 OK
    X-GUploader-UploadID: AEnB2UqLAR0Gf5xGi9pDGEYgMkrajFAuaC33IpiY-2WM2hspQe30DxUz2qzELRenLWOyrK8x9S3He51SXGmHU6olQzUGqGxbcw
    Location: https://www.googleapis.com/upload/youtube/v3/videos?uploadType=resumable&part=snippet,status&upload_id=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    ETag: "m2yskBQFythfE4irbTIeOgYYfBU/V3uOxfY0pSsHgtj2XMjOGKOAp3o"
    Vary: Origin
    Vary: X-Origin
    X-Goog-Correlation-Id: Z14VW6zFX9E
    Cache-Control: no-cache, no-store, max-age=0, must-revalidate
    Pragma: no-cache
    Expires: Mon, 01 Jan 1990 00:00:00 GMT
    Date: Fri, 16 Jun 2017 08:36:22 GMT
    Content-Length: 0
    Server: UploadServer
    Content-Type: text/html; charset=UTF-8
    Alt-Svc: quic=":443"; ma=2592000; v="38,37,36,35"

https://www.googleapis.com/youtube/...现在需要(https://不仅仅是 www(

也来自YouTube的API网站

在 API 列表中,确保 YouTube 数据 API v3 的状态为"开启"。

如果您的应用程序将使用任何需要用户授权的 API 方法,请阅读身份验证指南以了解如何实现 OAuth 2.0 授权。

最新更新