请注意,2022 年 6 月。Google 似乎更新了其验证要求,增加了额外的步骤(或否定该方法 - 取决于您的观点)。
查看最近的评论了解更多详情
这可以通过 Oauth2 Playground 来完成,网址为https://developers.google.com/oauthplayground https://developers.google.com/oauthplayground
Steps:-
- 创建 Google 帐户(例如[电子邮件受保护] /cdn-cgi/l/email-protection) - 如果您使用现有帐户,请跳过此步骤。
- 使用 API 控制台注册 mydriveapp (https://console.developers.google.com/apis/credentials/oauthclient?project=mydriveapp https://console.developers.google.com/apis/credentials/oauthclient?project=mydriveapp要不就https://console.developers.google.com/apis/ https://console.developers.google.com/apis/)
- 创建一组新的凭据。
Credentials/Create Credentials/OAuth Client Id
然后选择Web application
- Include https://developers.google.com/oauthplayground https://developers.google.com/oauthplayground作为有效的重定向 URI
- 记下客户端 ID(Web 应用程序)和客户端密钥
- 登录身份[电子邮件受保护] /cdn-cgi/l/email-protection
- 前往 Oauth2 游乐场
- 在设置(齿轮图标)中,设置
- OAuth 流程:服务器端
- 访问类型:离线
- 使用您自己的 OAuth 凭据:勾选
- 客户端 ID 和客户端密钥:来自第 5 步
- 单击步骤 1 并选择 Drive API v3https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive(话虽如此,该技术也适用于列出的任何 Google API)
- 单击授权 API。系统将提示您选择 Google 帐户并确认访问
- 单击步骤 2 和“将授权码兑换为令牌”
- 复制返回的刷新令牌并将其粘贴到您的应用程序、源代码或某种形式的存储中,您的应用程序可以从中检索它。
您的应用程序现在可以在无人值守的情况下运行,并按所述使用刷新令牌https://developers.google.com/accounts/docs/OAuth2WebServer#offline https://developers.google.com/accounts/docs/OAuth2WebServer#offline获取访问令牌。
注意。请注意,刷新令牌可能会被 Google 过期,这意味着您需要重复第 5 步以获取新的刷新令牌。其症状是当您尝试使用刷新令牌时返回无效授予。
注意2。如果您想要一个可以访问您自己的(并且only您自己的)驱动器帐户,无需编写仅运行一次的授权代码。只需跳过步骤 1,并在步骤 6 中将“my.drive.app”替换为您自己的电子邮件地址。确保您了解刷新令牌被盗时的安全影响。
请参阅下面伍迪的评论,其中他链接到此 Google 视频https://www.youtube.com/watch?v=hfWe1gPCnzc https://www.youtube.com/watch?v=hfWe1gPCnzc
.
.
.
下面是一个快速 JavaScript 例程,展示了如何使用 OAuth Playground 中的刷新令牌来列出一些云端硬盘文件。您可以简单地将其复制粘贴到 Chrome 开发控制台中,或者使用 Node.js 运行它。当然要提供你自己的凭据(下面的都是假的)。
function get_access_token_using_saved_refresh_token() {
// from the oauth playground
const refresh_token = "1/0PvMAoF9GaJFqbNsLZQg-f9NXEljQclmRP4Gwfdo_0";
// from the API console
const client_id = "559798723558-amtjh114mvtpiqis80lkl3kdo4gfm5k.apps.googleusercontent.com";
// from the API console
const client_secret = "WnGC6KJ91H40mg6H9r1eF9L";
// from https://developers.google.com/identity/protocols/OAuth2WebServer#offline
const refresh_url = "https://www.googleapis.com/oauth2/v4/token";
const post_body = `grant_type=refresh_token&client_id=${encodeURIComponent(client_id)}&client_secret=${encodeURIComponent(client_secret)}&refresh_token=${encodeURIComponent(refresh_token)}`;
let refresh_request = {
body: post_body,
method: "POST",
headers: new Headers({
'Content-Type': 'application/x-www-form-urlencoded'
})
}
// post to the refresh endpoint, parse the json response and use the access token to call files.list
fetch(refresh_url, refresh_request).then( response => {
return(response.json());
}).then( response_json => {
console.log(response_json);
files_list(response_json.access_token);
});
}
// a quick and dirty function to list some Drive files using the newly acquired access token
function files_list (access_token) {
const drive_url = "https://www.googleapis.com/drive/v3/files";
let drive_request = {
method: "GET",
headers: new Headers({
Authorization: "Bearer "+access_token
})
}
fetch(drive_url, drive_request).then( response => {
return(response.json());
}).then( list => {
console.log("Found a file called "+list.files[0].name);
});
}
get_access_token_using_saved_refresh_token();