问题描述
我想要在Google云端硬盘中获取文件列表,但是OAuth 2是我最好的选择。
I'm trying to get the list of files on Google Drive with curl, but OAuth 2 is getting the best of me.
这里有一些东西我试过:
Here are some of the things I tried:
curl -H "Authorization: Bearer $token" https://www.googleapis.com/drive/v2/files
其中$ token是一个460字符的字符串我使用:
Where $token is a 460 character string I got using:
https://www.google.com/accounts/ClientLogin
和此上传(其工作原理)。这是我收到的错误:
and this upload script (which works great). This is the error I received:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "authError",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Invalid Credentials"
}
}
也尝试了:
curl https://www.googleapis.com/drive/v2/files?key=apiKey
错误:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Login Required",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Login Required"
}
}
>
And:
curl -H "Authorization: GoogleLogin auth=${token}" "https://www.googleapis.com/drive/v2/files"
错误:
{
"error": {
"errors": [
{
"domain": "usageLimits",
"reason": "dailyLimitExceededUnreg",
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
"extendedHelp": "https://code.google.com/apis/console"
}
],
"code": 403,
"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
}
}
无论是JavaScript和PHP客户端库,似乎已针对用户提供日志/通过以授权应用程序的情况进行优化,我需要的是一种方法来列出单个帐户中的文件,每次。
I had little success with both the JavaScript and PHP client libraries, both seem optimized for the situation where the user provides log/pass in order to authorize the app. What I need is a way to list the files from a single account, every time.
推荐答案
对于未来的Google员工:
For future googlers:
如果你想救一个下午的痛苦,忘记Google的数据并浏览
If you want to save yourself an afternoon of pain, forget google's doumentation and head over here
它的主旨,因为我知道stackoverflow喜欢引用内容链接:
The gist of it, since I know stackoverflow prefers quoting the content to linking:
/ p>
In your browser:
https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fanalytics&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&client_id=1234567890.apps.googleusercontent.com
允许访问,当然,并复制代码,应该看起来像4 / v6xr77ewYqjkslsdUOKwAzu
Allow the access, of course, and copy the code which should look like 4/v6xr77ewYqjkslsdUOKwAzu
curl -H "Content-Type: application/x-www-form-urlencoded" -d 'code=4/v6xr77ewYqjkslsdUOKwAzu&client_id=1234567890.apps.googleusercontent.com&client_secret=xywzxywzxywzxywzxywz&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code' https://accounts.google.com/o/oauth2/token
你会得到一个像这样的JSON:
You’ll get a JSON like this one:
{
"access_token" : "ya29.AHES6Zkjhkjhahskjhskkskjh",
"token_type" : "Bearer",
"expires_in" : 3600,
"refresh_token" : "1/HH9E7k5D0jakjhsd7askdjh7899a8sd989"
}
如果您卷曲:
curl 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=ya29.AHES6Zkjhkjhahskjhskkskjh'
您会得到类似的结果:
{
"issued_to": "562211803675.apps.googleusercontent.com",
"audience": "562211803675.apps.googleusercontent.com",
"scope": "https://www.googleapis.com/auth/analytics",
"expires_in": 3556
}
完成
curl 'https://www.googleapis.com/analytics/v3/management/accounts?access_token=ya29.AHES6Zkjhkjhahskjhskkskjh
续订令牌
之前收到的refresh_token
You have to use the "refresh_token" received previously
curl -d "client_id=562211803675.apps.googleusercontent.com&client_secret=ZQxoOBGbvMGnZOYUrVIDXrgl&refresh_token=1/HH9E7k5D0jakjhsd7askdjh7899a8sd989&grant_type=refresh_token" https://accounts.google.com/o/oauth2/token
,你会得到一个新的access_token。
and you’ll get a new access_token.
这篇关于列出谷歌驱动器文件与curl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!