【问题标题】:OAuth for third party API on Jupyter?Jupyter上第三方API的OAuth?
【发布时间】:2021-03-10 18:31:54
【问题描述】:

我在 Jupyter 笔记本上使用 Python 进行数据分析,并希望访问使用 OAuth 的第三方 API (Mendeley)。 Heroku 上曾经有一个手动生成令牌的服务器的解决方法,但最近已停止使用。

这一定是一个非常普遍的问题,但我找不到支持它的维护库。大多数 Python OAuth 库都是服务器专用的;有一个得到很好支持的 JupyterHub-OAuthenticator,但 IFAICS 将 OAuth 用于不同的目的。

ipyauth 看起来是业务,但没有太多更新,也没有记录如何扩展它以提供新服务。这种情况通常意味着有更好的支持可用。

请问目前维护的 Jupyter-Python-ThirdPartyAPI 库是什么?

【问题讨论】:

    标签: python oauth jupyter-notebook


    【解决方案1】:

    嗯,一个答案原来只是使用requests package,并且每次都复制并粘贴重定向的 URL:

    from requests_oauthlib import OAuth2Session
    scope = 'all'
    redirect_uri='http://localhost:8888/'
    oauth = OAuth2Session('YourApplicationApiIdNumber', redirect_uri=redirect_uri, scope=scope)
    authorization_url, state = oauth.authorization_url(
            "https://api.mendeley.com/oauth/authorize" )
    
    print( 'Please go to %s to authorize access, and copy the final localhost URL' % authorization_url )
    assert(False) # Stop processing until this is done.
    

    ...变成一个变量:

    authorization_response = 'http://localhost:8888/tree?TheStuffWereInterestedIn'
    

    ...然后从那里拿走:

    token = oauth.fetch_token(
            'https://api.mendeley.com/oauth/token',
            authorization_response=authorization_response.replace('http', 'https').replace(',',''),
            client_secret='YourClientSecret')
    r = oauth.get('https://api.mendeley.com/documents?sort=last_modified&order=desc&limit=500', timeout=30)
    

    您必须使用回调 URL 配置 Mendeley 应用程序界面。这是 http://localhost:8888/ ,因为这是 Jupyter 可以在不丢失其他 OAuth2 参数的情况下显示的内容。但是 Requests OAuth 实现不接受非 https 链接(也不接受 Jupyter 偶尔添加的尾随逗号),因此我们如图所示对其进行了篡改。

    我猜这种方法几乎适用于任何 OAuth2 API。当然请求列表quite a few

    【讨论】:

    • 我之前的解决方案需要一段时间才能执行,因为必须等待 https 请求超时。相反,jupyter 对 localhost:8888/?Anything 很满意,并保留了任何参数。解决方案相应修改。
    猜你喜欢
    • 1970-01-01
    • 2019-10-09
    • 2020-10-13
    • 2012-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-30
    • 2021-05-03
    相关资源
    最近更新 更多