使用访问令牌将文件上传到Google云端硬盘,需要什么范围的身份验证? [英] Upload file to Google Drive using access token, What scope of authentication is required?
问题描述
我尝试使用PHP将文件上传到Google云端硬盘。
I am trying to upload a file to Google Drive with PHP.
对于授权码,我使用以下范围:
For the authorization code, I am using the following scopes:
https://www.googleapis.com/auth/drive
https://docs.google.com/feeds/
https://docs.googleusercontent.com/
https://spreadsheets.google.com/feeds/
我已经收到一个有效的 refresh_token
,我可以使用它来获取一个新的 access_token
I already received a valid refresh_token
, and I'm able to use it to fetch a new access_token
anytime I wish.
现在的问题是,当我试图使用 access_token
strong>未展开,尚未使用)它说:
Now the problem is when I am trying to perform the upload using this access_token
(it is not exprired, and not used yet) it says:
stdClass Object
(
[error] => stdClass Object
(
[errors] => Array
(
[0] => stdClass Object
(
[domain] => global
[reason] => required
[message] => Login Required
[locationType] => header
[location] => Authorization
)
)
[code] => 401
[message] => Login Required
)
)
我使用curl和下面的标题: / p>
I am using curl with following header:
Array
(
[0] => Content-Type: text/plain
[1] => Content-Length: 503242
[2] => Authorization: Bearer ya29.AHES6ZRWXZkZDDi6AFV9PK2QonHp93nIfJbYqipXFT1uCsg
[3] => GData-Version: 3.0
)
和以下网址:
https://www.googleapis.com/upload/drive/v2/files?uploadType=media
CURL命令如下:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADERS, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($file));
$return = curl_exec($ch);
curl_close($ch);
任何帮助将被衷心感谢...
提前感谢...
Any Help will be heartly appreciated... Thanks in advance...
推荐答案
将文件上传到云端硬盘所需的唯一OAuth范围是 https://www.googleapis。 com / auth / drive.file
。
The only OAuth scope you need to upload a file to Drive is https://www.googleapis.com/auth/drive.file
.
对于所有可能的范围,请查看文档: https ://developers.google.com/drive/scopes
For all possible scopes, check the documentation: https://developers.google.com/drive/scopes
顺便说一下,Drive API不是基于GData的,因此您不需要
By the way, the Drive API is not GData-based so you don't need to add the GData-Version
header to your requests.
我也建议您比较您的要求与您的要求。由OAuth 2.0 Playground发送,因此应该很容易发现错误:
I'd also recommend you to compare your requests with those sent by the OAuth 2.0 Playground so that it should be easy to spot the error:
https://developers.google.com/oauthplayground/
这篇关于使用访问令牌将文件上传到Google云端硬盘,需要什么范围的身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!