gpt4 book ai didi

php - 请求的授权 token 是什么? (Youtube API)

转载 作者:行者123 更新时间:2023-12-03 05:27:42 27 4
gpt4 key购买 nike

在尝试使用完整的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 Forbidden错误。我认为问题出在授权 token 上。我认为我没有使用应有的功能。谷歌说

"The request sets the following HTTP request headers:

  • Authorization – The authorization token for the request."

我不确定它到底是什么,所以我使用了由Youtube PHP Client API生成的访问 token ,但仍然没有。

最佳答案

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

这样,您可以在响应头中获取location参数。请参阅下面的代码和我得到的响应头。另外,在JSON中使用干净的$requestBody数据

<?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的位置。下一步,请访问此链接 Save the resumable session 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"

关于php - 请求的授权 token 是什么? (Youtube API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44461080/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com