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

时间:2013-11-04 11:37:23

标签: google-api google-drive-api google-oauth gmail-api

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

我知道我的应用需要刷新令牌,但我不想编写代码来获取,因为我只会做一次。

NB。这不是使用服务帐户。该应用程序将在传统的Google帐户下运行。在某些情况下,服务帐户是一种有效的方法。但是,使用Oauth Playground模拟应用程序的技术可以节省大量的冗余工作,并适用于不支持与服务帐户共享的任何API。

2 个答案:

答案 0 :(得分:112)

这可以通过https://developers.google.com/oauthplayground

的Oauth2游乐场来完成

步骤: -

  1. 创建Google帐户(例如my.drive.app@gmail.com) - 如果您使用的是现有帐户,请跳过此步骤。
  2. 使用API​​控制台注册mydriveapp(https://console.developers.google.com/apis/credentials/oauthclient?project=mydriveapphttps://console.developers.google.com/apis/
  3. 创建一组新凭据。 Credentials/Create Credentials/OAuth Client Id然后选择Web application
  4. https://developers.google.com/oauthplayground包含为有效的重定向URI
  5. 请注意客户端ID(网络应用)和客户端密钥
  6. 以my.drive.app@gmail.com
  7. 身份登录
  8. 前往Oauth2游乐场
  9. 在设置(齿轮图标)中,设置
    • Oauth flow:服务器
    • 访问类型:离线
    • 使用您自己的OAuth凭据:TICK
    • 客户端ID和客户端密钥:来自第5步
  10. 点击第1步,然后选择云端硬盘API https://www.googleapis.com/auth/drive(尽管如此,此技术也可用于列出的任何Google API)
  11. 单击授权API。系统会提示您选择自己的Google帐户并确认访问权限
  12. 点击第2步和“令牌交换授权码”
  13. 复制返回的刷新令牌并将其粘贴到您的应用,源代码或应用程序可以从中检索它的某种形式的存储中。
  14. 您的应用现在可以无人值守运行,并使用https://developers.google.com/accounts/docs/OAuth2WebServer#offline所述的刷新令牌来获取访问令牌。

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

    NB2。如果您想要一个可以访问您自己的(并且您自己的)云端硬盘帐户的网络应用程序,而无需编写只能运行一次的授权代码,这种方法效果很好。只需跳过步骤1,并在步骤6中将“my.drive.app”替换为您自己的电子邮件地址。确保您知道如果刷新令牌被盗会对安全性产生影响。

    请参阅下面的Woody评论链接到此Google视频https://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();
    

答案 1 :(得分:1)

让我为pinoyyid的出色答案(这对我不起作用-弹出重定向错误)添加一条替代路线。

除了使用OAuthPlayground之外,您还可以直接使用HTTP REST API。因此,与pinoyyid答案不同的是,我们将在本地进行操作。按照pinoyyid的答案执行步骤1-3。我会引用它们:

  1. 创建Google帐户(例如,my.drive.app @ gmail.com)-如果您使用的是现有帐户,则跳过此步骤。
  2. 使用API​​控制台注册mydriveapp(https://console.developers.google.com/apis/credentials/oauthclient?project=mydriveapp或仅注册https://console.developers.google.com/apis/
  3. 创建一组新的凭据(NB OAuth客户端ID不是服务帐户密钥,然后从选择中选择“ Web应用程序”)

现在,将以下内容而不是操场添加到您的凭据中:

授权的JavaScript来源: http://localhost(我不知道这是否是必需的,只是这样做。)
授权的重定向URI: http://localhost:8080

屏幕截图(德语):

OAuth source/redirect settings

请确保通过下面的蓝色按钮实际保存

现在,您可能需要使用GUI来构建HTTP请求。我使用了Insomnia,但是您可以使用Postman或纯cURL。我建议您使用失眠症,因为它可以让您轻松浏览同意书。

使用以下参数构建新的 GET 请求:

URL: https://accounts.google.com/o/oauth2/v2/auth
Query Param: redirect_uri=http://localhost:8080
Query Param: prompt=consent
Query Param: response_type=code
Query Param: client_id=<your client id from OAuth credentials>
Query Param: scope=<your chosen scopes, e.g. https://www.googleapis.com/auth/drive.file>
Query Param: access_type=offline

如果您选择的工具不能自动处理URL编码,请确保正确使用它。

在触发请求之前,请设置网络服务器以监听http://localhost:8080。如果您已安装节点并运行了npm,请运行npm i express,然后创建一个index.js

var express = require('express');
var app = express();

app.get('/', function (req, res) {
  res.send('ok');
  console.log(req)
});

app.listen(8080, function () {
  console.log('Listening on port 8080!');
});

然后通过node index.js运行服务器。我建议不要记录整个req对象,也不要运行node index.js | less,因为完整的输出将非常庞大。
对于其他语言,也有非常简单的解决方案。例如。在8080 php -S localhost:8080上使用PHP的内置Web服务器。

现在(在失眠状态下)发出您的请求,并提示您输入登录名:

login prompt

使用您的电子邮件和密码登录并确认同意屏幕(应包含您选择的范围)。

返回到您的终端并检查输出。如果您记录了整个内容,请向下滚动(例如在pgdown中向下滚动),直到看到带有code=4/...的行。

复制该代码;您将需要使用该授权码来交换访问和刷新令牌。不要复制太多-如果有&符号&,请不要复制它或之后的任何内容。 &分隔查询参数。我们只想要code

现在设置一个指向https://www.googleapis.com/oauth2/v4/token的HTTP POST请求,该请求为格式的URL 。在Insomnia中,您只需单击即可-在其他工具中,您可能必须将标题设置为Content-Type: application/x-www-form-urlencoded

添加以下参数:

code=<the authorization code from the last step>
client_id=<your client ID again>
client_secret=<your client secret from the OAuth credentials>
redirect_uri=http://localhost:8080
grant_type=authorization_code

再次,确保编码正确。

触发您的请求并检查服务器的输出。在响应中,您应该看到一个JSON对象:

{
  "access_token": "xxxx",
  "expires_in": 3600,
  "refresh_token": "1/xxxx",
  "scope": "https://www.googleapis.com/auth/drive.file",
  "token_type": "Bearer"
}

您可以立即使用access_token,但仅可使用一小时。注意刷新令牌。您可以始终*交换新的访问令牌。

*,如果用户更改密码,撤消访问权限,6个月内未激活等,则必须重复该过程。

快乐的 OAuthing

相关问题