使用刷新和访问令牌通过 google api v3 (php) 上传到 youtube
Upload to youtube via google api v3 (php) with refresh and access token
我想开发一个 restful 应用程序,用户可以通过一些管理界面将视频上传到 YouTube。由于用户只会代表我的名字上传,并且在一个频道中我只想进行一次身份验证,然后使用刷新令牌来获取访问令牌。
所以我做的是下面的
- 我访问了 https://developers.google.com 和 select 并使用我的电子邮件
授权了所有 Youtube 数据 API v3 API
- 交换令牌的授权码(所以现在我有授权码、刷新和访问令牌)
- 代码实现(卡在这里,无法想象是吧?)
$client = new Google_Client();
$client->setApplicationName('myApp');
$client->setClientId('<client-id>');
$client->setClientSecret('<client-secret>');
$client->setDeveloperKey('<dev-key>'); // <- do I really need that
$client->setScopes('https://www.googleapis.com/auth/youtube.force-ssl https://www.googleapis.com/auth/youtube.upload https://www.googleapis.com/auth/youtubepartner https://www.googleapis.com/auth/youtube https://www.googleapis.com/auth/youtubepartner-channel-audit https://www.googleapis.com/auth/youtube.readonly');
$client->refreshToken('<my-refresh-token>');
$client->setAuthConfig('client_secrets.json'); // <- is that the same as setting clientId and ClientSecret???
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$accessToken = $client->getAccessToken();
if (is_null($accessToken) || $client->isAccessTokenExpired()) {
// How to refresh token with REFRESH token?
dd($_GET);
}
// Define service object for making API requests.
$service = new Google_Service_YouTube($client);
// Define the $video object, which will be uploaded as the request body.
$video = new Google_Service_YouTube_Video();
// Add 'snippet' object to the $video object.
$videoSnippet = new Google_Service_YouTube_VideoSnippet();
$videoSnippet->setCategoryId('1');
$videoSnippet->setChannelId('<my-channel-id>');
$videoSnippet->setDescription('Description of uploaded video.');
$videoSnippet->setTags(['tag', 'tag2', 'tag3']);
$videoSnippet->setTitle('Test video upload.');
$video->setSnippet($videoSnippet);
// Add 'status' object to the $video object.
$videoStatus = new Google_Service_YouTube_VideoStatus();
$videoStatus->setEmbeddable(true);
$videoStatus->setLicense('youtube');
$videoStatus->setPrivacyStatus('private');
$video->setStatus($videoStatus);
$queryParams = [
'stabilize' => false
];
// TODO: For this request to work, you must replace "YOUR_FILE"
// with a pointer to the actual file you are uploading.
// The maximum file size for this operation is 64GB.
$response = $service->videos->insert(
'snippet,status',
$video,
$queryParams,
array(
'data' => file_get_contents($fullFilePath),
'mimeType' => 'video/*',
'uploadType' => 'multipart'
)
);
print_r($response);
所以现在我有几个问题,我不知道如何解决。
- 如果我已经获得授权(通过 OAuth 游乐场),则必须存在哪些
$client->....
函数
- 如何使用刷新令牌刷新令牌?
到目前为止我得到的唯一回应是 Google_Service_Exception
Message: { "error": { "errors": [ { "domain": "global", "reason": "required", "message": "Login Required", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Login Required" } }
这是我尝试通过 api 使用 PHP 上传视频的第二天,这让我抓狂。希望大家帮帮我。
如果您需要任何其他信息,请告诉我,我会提供。谢谢!!
更新
添加以下代码后
$client->setAccessToken('<ACCESS_TOKEN>');
我收到以下错误
div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
<h4>An uncaught Exception was encountered</h4>
<p>Type: Google_Service_Exception</p>
<p>Message: {
"error": {
"errors": [
{
"domain": "youtube.quota",
"reason": "quotaExceeded",
"message": "The request cannot be completed because you have exceeded your \u003ca href=\"/youtube/v3/getting-started#quota\"\u003equota\u003c/a\u003e."
}
],
"code": 403,
"message": "The request cannot be completed because you have exceeded your \u003ca href=\"/youtube/v3/getting-started#quota\"\u003equota\u003c/a\u003e."
}
}
</p>
<p>Filename: /home/vagrant/workspace/spot-scouting-adminpage/rest/vendor/google/apiclient/src/Google/Http/REST.php</p>
<p>Line Number: 118</p>
这当然不是真的,因为我从来没有向 google 发出过一次成功的请求。这是教授:
也许问题是通过 developers.google.com 生成了访问密钥???
您错过了设置访问令牌的一小步。
获得访问令牌后,将其设置为 google 客户端:
$client->setAccessToken($accessToken);
然后使用youtube服务:
$service = new Google_Service_YouTube($client);
我想开发一个 restful 应用程序,用户可以通过一些管理界面将视频上传到 YouTube。由于用户只会代表我的名字上传,并且在一个频道中我只想进行一次身份验证,然后使用刷新令牌来获取访问令牌。
所以我做的是下面的
- 我访问了 https://developers.google.com 和 select 并使用我的电子邮件 授权了所有 Youtube 数据 API v3 API
- 交换令牌的授权码(所以现在我有授权码、刷新和访问令牌)
- 代码实现(卡在这里,无法想象是吧?)
$client = new Google_Client();
$client->setApplicationName('myApp');
$client->setClientId('<client-id>');
$client->setClientSecret('<client-secret>');
$client->setDeveloperKey('<dev-key>'); // <- do I really need that
$client->setScopes('https://www.googleapis.com/auth/youtube.force-ssl https://www.googleapis.com/auth/youtube.upload https://www.googleapis.com/auth/youtubepartner https://www.googleapis.com/auth/youtube https://www.googleapis.com/auth/youtubepartner-channel-audit https://www.googleapis.com/auth/youtube.readonly');
$client->refreshToken('<my-refresh-token>');
$client->setAuthConfig('client_secrets.json'); // <- is that the same as setting clientId and ClientSecret???
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$accessToken = $client->getAccessToken();
if (is_null($accessToken) || $client->isAccessTokenExpired()) {
// How to refresh token with REFRESH token?
dd($_GET);
}
// Define service object for making API requests.
$service = new Google_Service_YouTube($client);
// Define the $video object, which will be uploaded as the request body.
$video = new Google_Service_YouTube_Video();
// Add 'snippet' object to the $video object.
$videoSnippet = new Google_Service_YouTube_VideoSnippet();
$videoSnippet->setCategoryId('1');
$videoSnippet->setChannelId('<my-channel-id>');
$videoSnippet->setDescription('Description of uploaded video.');
$videoSnippet->setTags(['tag', 'tag2', 'tag3']);
$videoSnippet->setTitle('Test video upload.');
$video->setSnippet($videoSnippet);
// Add 'status' object to the $video object.
$videoStatus = new Google_Service_YouTube_VideoStatus();
$videoStatus->setEmbeddable(true);
$videoStatus->setLicense('youtube');
$videoStatus->setPrivacyStatus('private');
$video->setStatus($videoStatus);
$queryParams = [
'stabilize' => false
];
// TODO: For this request to work, you must replace "YOUR_FILE"
// with a pointer to the actual file you are uploading.
// The maximum file size for this operation is 64GB.
$response = $service->videos->insert(
'snippet,status',
$video,
$queryParams,
array(
'data' => file_get_contents($fullFilePath),
'mimeType' => 'video/*',
'uploadType' => 'multipart'
)
);
print_r($response);
所以现在我有几个问题,我不知道如何解决。
- 如果我已经获得授权(通过 OAuth 游乐场),则必须存在哪些
$client->....
函数 - 如何使用刷新令牌刷新令牌?
到目前为止我得到的唯一回应是 Google_Service_Exception
Message: { "error": { "errors": [ { "domain": "global", "reason": "required", "message": "Login Required", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Login Required" } }
这是我尝试通过 api 使用 PHP 上传视频的第二天,这让我抓狂。希望大家帮帮我。
如果您需要任何其他信息,请告诉我,我会提供。谢谢!!
更新
添加以下代码后
$client->setAccessToken('<ACCESS_TOKEN>');
我收到以下错误
div style="border:1px solid #990000;padding-left:20px;margin:0 0 10px 0;">
<h4>An uncaught Exception was encountered</h4>
<p>Type: Google_Service_Exception</p>
<p>Message: {
"error": {
"errors": [
{
"domain": "youtube.quota",
"reason": "quotaExceeded",
"message": "The request cannot be completed because you have exceeded your \u003ca href=\"/youtube/v3/getting-started#quota\"\u003equota\u003c/a\u003e."
}
],
"code": 403,
"message": "The request cannot be completed because you have exceeded your \u003ca href=\"/youtube/v3/getting-started#quota\"\u003equota\u003c/a\u003e."
}
}
</p>
<p>Filename: /home/vagrant/workspace/spot-scouting-adminpage/rest/vendor/google/apiclient/src/Google/Http/REST.php</p>
<p>Line Number: 118</p>
这当然不是真的,因为我从来没有向 google 发出过一次成功的请求。这是教授:
也许问题是通过 developers.google.com 生成了访问密钥???
您错过了设置访问令牌的一小步。 获得访问令牌后,将其设置为 google 客户端:
$client->setAccessToken($accessToken);
然后使用youtube服务:
$service = new Google_Service_YouTube($client);