如何在没有用户干预的情况下授权应用程序(网页或安装)? (规范?)

假设我有一个需要在后台服务中访问云端硬盘文件的networking应用程序。 它将拥有自己正在访问的文件,也可以在拥有者共享文档的Google帐户中运行。

我明白,我的应用程序需要一个刷新令牌,但我不想写代码来获取,因为我只会做一次。

NB。 这不是使用服务帐户。 该应用将在传统的Google帐户下运行。 我不是说这是一个好主意,在某些情况下,服务帐户是一种有效的方法。 然而,使用Oauth Playground模拟应用程序的技术可以节省大量的重复劳动。

这可以通过https://developers.google.com/oauthplayground上的Oauth2 Playground完成

脚步:-

  1. 创buildGoogle帐户(例如my.drive.app@gmail.com)
  2. 使用API​​控制台注册mydrive应用程序( https://console.developers.google.com/apis/credentials/oauthclient?project=mydriveapp或仅https://console.developers.google.com/apis/
  3. 创build一组新的证书(NB OAuth Client ID 不是 Service Account Key ,然后从select中select“Web应用程序”)
  4. 包括https://developers.google.com/oauthplayground作为有效的redirectURI
  5. 请注意客户端ID(Web应用程序)和客户端密钥
  6. 以my.drive.app@gmail.comlogin
  7. 去Oauth2游乐场
  8. 在设置(齿轮图标)中设置
    • Oauthstream量:服务器
    • 访问types:脱机
    • 使用您自己的OAuth凭据:TICK
    • 客户端ID和客户端密钥:从第5步开始
  9. 点击第1步,然后selectDrive API https://www.googleapis.com/auth/drive (话虽如此,这种技术也适用于任何列出的Google API)
  10. 点击授权API。 系统会提示您select您的Google帐户并确认访问权限
  11. 单击“步骤2”和“交换令牌的授权代码”
  12. 复制返回的刷新令牌并将其粘贴到您的应用程序,源代码或从您的应用程序可以检索到的某种forms的存储中。

您的应用现在可以无人值守运行,并按照https://developers.google.com/accounts/docs/OAuth2WebServer#offline所述使用刷新令牌来获取访问令牌。;

NB。 请注意,刷新令牌可能会由Google过期,这意味着您需要重复步骤5以获取新的刷新令牌。 当您尝试使用刷新令牌时,此症状将是返回的无效授予。

NB2。 如果您想要一个访问您自己的(并且只有您自己的)云端硬盘帐户的Web应用程序,而无需编写只能运行一次的授权代码,这种技术运行良好。 只需跳过第1步,在步骤5中用您自己的电子邮件地址replace“my.drive.app”。确保您知道刷新令牌被盗的安全隐患。

请参阅下面的Woody的评论,他链接到此Googlevideohttps://www.youtube.com/watch?v=hfWe1gPCnzc

。 。 。

以下是一个快速的JavaScript例程,展示了如何使用OAuth Playground中的Refresh Token列出一些Drive文件。 您可以简单地将其复制粘贴到Chrome开发人员控制台中,或者使用节点运行。 当然提供你自己的凭据(下面的都是假的)。

 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();