问题描述
我正在尝试使用 curl 获取 Google 云端硬盘上的文件列表,但 OAuth 2 对我来说是最好的.
以下是我尝试过的一些事情:
curl -H "授权: Bearer $token" https://www.googleapis.com/drive/v2/files
其中 $token 是我使用的 460 个字符的字符串:
https://www.google.com/accounts/ClientLogin
这个上传
记下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" : "KRYU-NTVW4/qi6ysOpK2AtsmtZz4MB9LAthlYGGgAepxpBnGQLvhqo","user_code" : "KRYU-NTVW","verification_url" : "https://www.google.com/device",expires_in":1800,间隔":5}
复制user_code
.
第 3 步:授权请求.
访问验证网址
第四步:获取access_token
.
将auth请求得到的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": "承载者",expires_in":3600,"refresh_token": "1/qinFVaMPYvhWtUtmjb1qCpnQt48XyvQhB_ILZJ4H1Uw"}
我们现在有了所需的 access_token
,保存它并将其用于所有驱动器 REST API 请求.
第 5 步:驱动 API 请求.
$ curl -H 'GData-Version: 3.0' -H '授权:承载 ya29.kgKW4Z4IDqK7lCjUQw-u5VT2uAx19MtgdoKeAC9ikKYGwKh7Nh46pY8nQsANQ5lRwA' https://www.googleapis.com/drive/v2/files{种类":驱动器#fileList",...
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
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"
}
}
Also tried:
curl https://www.googleapis.com/drive/v2/files?key=apiKey
Error:
{
"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:
{
"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."
}
}
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.
I just spent like 30 minutes to figure this out myself for accessing contacts API and redid the steps for drive API and documented them for future reference. There are 5 steps and the first 4 are one time setup.
Step 1: Create new OAuth2 credentials
Note down the client ID
and client secret
.
Step 2: Request authorization with drive as scope.
Using the client ID
and https://docs.google.com/feeds
as scope
, construct the below curl command:
$ 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
}
Copy the user_code
.
Step 3: Authorize the request.
Visit the verification URL https://www.google.com/device and enter the copied code.
Step 4: Obtain access_token
.
Add the device_code
obtained from the auth request to the client ID
and client secret
and construct the below curl command:
$ 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"
}
We now have the required access_token
, save it and use it with all the drive REST API requests.
Step 5: Drive API request.
$ curl -H 'GData-Version: 3.0' -H 'Authorization: Bearer ya29.kgKW4Z4IDqK7lCjUQw-u5VT2uAx19MtgdoKeAC9ikKYGwKh7Nh46pY8nQsANQ5lRwA' https://www.googleapis.com/drive/v2/files
{
"kind": "drive#fileList",
...
这篇关于使用 curl 列出谷歌驱动器文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!