google-api - 使用 Python Google API 进行身份验证以访问自己的帐户的最简单方法

标签 google-api google-oauth google-authentication google-api-python-client headless-browser

主要用例是使用 IPython 作为我自己的 Google 帐户的 CLI。我真正追求的是尽量减少启动 IPython shell 和实际发出针对 API 的有用调用之间的麻烦。

使用 Google API 进行身份验证的文档侧重于设置其他用户将用来访问其数据的应用程序。
这导致了涉及浏览器的冗长 Oauth 舞蹈,以允许其他用户在不损害其凭据的情况下进行身份验证。
但是,我不介意与自己分享我的私有(private)凭据。我不打算分享代码。如果我确实分享了代码,我会使用 dotenv 之类的东西将凭据与代码分开。

Twitter 为开发者提供第二套凭证
这允许开发人员访问他们自己的帐户进行测试。
因此可以以编程方式访问自己的帐户
只需提供一组凭据:允许调用 API 的开发人员凭据和授予对开发人员自己数据的访问权限的其他凭据。对于example :

from twitter import *

t = Twitter(
    auth=OAuth(token, token_key, con_secret, con_secret_key))

# Get your "home" timeline
t.statuses.home_timeline()

# Update your status
t.statuses.update(
    status="Tweeting from Python")

在哪里 con_secret*是开发人员凭据和
token*是帐户访问凭据。
  • 如何使用 Google API 做同样简单的事情?
  • 我在哪里可以获得访问我自己帐户的凭据?
  • 我将如何在 Google API 中使用它们?

  • 例如,从我自己的一个 Youtube 播放列表中检索内容的最简单程序是什么?

    我认为 Python headless 浏览器库可以满足我的需求。我问了一个关于 SE Software Recommendations 的相关问题
    https://softwarerecs.stackexchange.com/questions/35744/python-headless-browser-library-for-oauth2-authentication-from-ipython-console

    最佳答案

    I would like to download a set of credentials



    Google 通过其 client_secrets.json 提供此功能文件。根据您要使用的帐户类型(Web 应用程序、已安装的应用程序、服务帐户),有不同的下载方式。可以找到不同的技术 here .

    Store the credentials locally and keep using them without requiring new credentials every call



    这也不是问题,客户端密码在您续订之前一直有效 - AFAIK 除非您另行指定,否则不会自动到期。

    一旦你下载了client_secrets.json ,将文件存储在非公共(public)目录中(通常在您的项目目录/配置中)。

    与文件的下载类似,有不同的技术(流类)来使用 JSON 文件,具体取决于您使用的帐户类型。例如,以下将用于已安装和 Web 应用程序;
    from oauth2client.client import OAuth2WebServerFlow
    ...
    flow = OAuth2WebServerFlow(client_id='your_client_id',
                               client_secret='your_client_secret',
                               scope='scope URL here',
                               redirect_uri='http://example.com/auth_return')
    

    其他流类示例可见here

    希望这会有所帮助 - 如果您需要更多信息,可以在此处找到官方文档(被警告,可能非常不准确和令人困惑)https://developers.google.com/api-client-library/python/guide/aaa_oauth

    关于google-api - 使用 Python Google API 进行身份验证以访问自己的帐户的最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39047595/

    相关文章:

    laravel - 为什么我的 Google Drive Refresh Token 会在 7 天后过期?

    authentication - gcloud auth 不适用于 Windows WSL

    google-api - 使用 OAuth2 服务帐户生成访问 token 时出现问题

    angular - Angular 6 中的谷歌身份验证

    php - Google API 如何连接以从电子表格接收值

    java - SpreadsheetEntry.getKey() 返回前面带有 'spreadsheet%3A' 的键

    oauth-2.0 - 无法再获取 OAuth2 访问 token : invalid_scope error

    oauth-2.0 - Gmail 上下文小工具(Gmail 插件/扩展程序): Deprecated? 不可能?如何? (不能做 OAuth 2.0)

    node.js - Gmail API 是否支持 JWT?

    Java - 具有 2 条腿 Oauth 的 Google Calendar API v3 配额