Skip to main content
 首页 » 编程设计

google-api之如何在没有用户干预的情况下授权应用程序(网络或安装)

2024年02月27日18lvdongjie

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

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

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

请您参考如下方法:

注意,2022 年 6 月。Google 似乎更新了其验证要求,增加了额外的步骤(或否定该方法 - 取决于您的观点)。 有关更多详细信息,请参阅最近的评论

这可以通过位于 https://developers.google.com/oauthplayground 的 Oauth2 Playground 来完成。

步骤:-

  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. 创建一组新的凭据。 Credentials/Create Credentials/OAuth Client Id,然后选择Web application
  4. 包括https://developers.google.com/oauthplayground作为有效的重定向 URI
  5. 记下客户端 ID(网络应用)和客户端 key
  6. 以 my.drive.app@gmail.com 身份登录
  7. 前往 Oauth2 Playground
  8. 在“设置”(齿轮图标)中进行设置
  • OAuth 流程:服务器端
  • 访问类型:离线
  • 使用您自己的 OAuth 凭据:勾选
  • 客户端 ID 和客户端 key :来自第 5 步
  1. 点击第 1 步并选择 Drive API v3 https://www.googleapis.com/auth/drive (话虽如此,此技术也适用于列出的任何 Google API)
  2. 点击授权 API。系统将提示您选择 Google 帐户并确认访问
  3. 点击第 2 步和“将授权代码兑换为 token ”
  4. 复制返回的刷新 token 并将其粘贴到您的应用、源代码或某种形式的存储中,您的应用可以从其中检索它。

您的应用现在可以在无人值守的情况下运行,并使用刷新 token ,如 https://developers.google.com/accounts/docs/OAuth2WebServer#offline 中所述。获取访问 token 。

注意。请注意,刷新 token 可能会被 Google 过期,这意味着您需要重复第 5 步以获取新的刷新 token 。其症状是当您尝试使用刷新 token 时返回无效授予。

NB2。如果您想要一个可以访问您自己的(并且您自己的)云端硬盘帐户的网络应用程序,并且无需编写仅运行一次的授权代码,则此技术非常有效。只需跳过第 1 步,并在第 6 步中将“my.drive.app”替换为您自己的电子邮件地址。请确保您了解刷新 token 被盗时的安全隐患。

请参阅下面 Woody 的评论,其中他链接到此 Google 视频 https://www.youtube.com/watch?v=hfWe1gPCnzc

。 。 .

这是一个快速的 JavaScript 例程,展示了如何使用 OAuth Playground 中的刷新 token 来列出一些云端硬盘文件。您可以简单地将其复制粘贴到 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();