我正在尝试使用curl获取Google云端硬盘上的文件列表,但OAuth 2正在让我感觉最好。
以下是我尝试过的一些事情:
curl -H "Authorization: Bearer $token" https://www.googleapis.com/drive/v2/files
其中$ token是我使用的460个字符的字符串:
https://www.google.com/accounts/ClientLogin
并上传script(效果很好)。这是我收到的错误:
{
"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"
}
}
和
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客户端库上都没什么成功,两者似乎都针对用户提供日志/传递以授权应用程序的情况进行了优化。我需要的是每次都从单个帐户列出文件的方法。
答案 0 :(得分:7)
我花了大约30分钟时间来弄清楚自己访问联系人API并重新编写驱动程序API的步骤并将其记录下来以供将来参考。共有5个步骤,前4个是一次性设置。
第1步:创建新的OAuth2凭据
记下client ID
和client secret
。
第2步:以驱动器作为范围请求授权。
使用client ID
和https://docs.google.com/feeds
作为scope
,构建以下curl命令:
$ curl -d "client_id=413437979384-05efiod756k5hp2eji5tsn2lmlg0qslc.apps.googleusercontent.com&scope=https://docs.google.com/feeds" https://accounts.google.com/o/oauth2/device/code
{
"device_code" : "KRYU-NTVW4/qi6ysOpK2AtsmtZz4MB9LAthlYGGgAepxpBnGQLvhqo",
"user_code" : "KRYU-NTVW",
"verification_url" : "https://www.google.com/device",
"expires_in" : 1800,
"interval" : 5
}
复制user_code
。
第3步:授权请求。
访问验证网址https://www.google.com/device并输入复制的代码。
第4步:获取access_token
。
将从身份验证请求中获取的device_code
添加到client ID
和client secret
并构建以下curl命令:
$ curl -d "client_id=413437979384-05efiod756k5hp2eji5tsn2lmlg0qslc.apps.googleusercontent.com&client_secret=0zWNribRJ4PcYWH-rDkCpCcm&grant_type=http://oauth.net/grant_type/device/1.0&code=KRYU-NTVW4/qi6ysOpK2AtsmtZz4MB9LAthlYGGgAepxpBnGQLvhqo" https://www.googleapis.com/oauth2/v4/token
{
"access_token": "ya29.kgKW4Z4IDqK7lCjUQw-u5VT2uAx19MtgdoKeAC9ikKYGwKh7Nh46pY8nQsANQ5lRwA",
"token_type": "Bearer",
"expires_in": 3600,
"refresh_token": "1/qinFVaMPYvhWtUtmjb1qCpnQt48XyvQhB_ILZJ4H1Uw"
}
我们现在拥有所需的access_token
,将其保存并与所有驱动器REST API请求一起使用。
第5步:驱动API请求。
$ curl -H 'GData-Version: 3.0' -H 'Authorization: Bearer ya29.kgKW4Z4IDqK7lCjUQw-u5VT2uAx19MtgdoKeAC9ikKYGwKh7Nh46pY8nQsANQ5lRwA' https://www.googleapis.com/drive/v2/files
{
"kind": "drive#fileList",
...
答案 1 :(得分:5)
未来的googlers:
如果你想为自己节省一个下午的痛苦,请忘记google的doumentation并转过here
它的要点,因为我知道stackoverflow更喜欢引用内容链接:
在您的浏览器中:
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的代码
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:
{
"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”
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。