python - 如何自动对 basecamp 3 api 进行身份验证(最好在 python 中)?

标签 python oauth-2.0 basecamp

这是我第一次涉足 OAuth,所以如果我离题太远,或者方向完全错误,请耐心等待。

我想编写一个脚本来从 basecamp 3 中提取信息,对其进行格式化,然后通过电子邮件发送。我已经用 basecamp 2 做到了,而且效果很好。但是 basecamp 3 不允许简单的身份验证。这只是一个每周通过 cron 运行一次的脚本。

我发现的大多数 OAuth 示例都需要获取授权 URL、在浏览器中访问它、授予授权等,以便获得访问 token 。请告诉我有一个自动化的方式!我不能让这成为一个手动过程。

我尝试使用 requests-oauthlib 的后端应用程序流程(在此处找到:https://requests-oauthlib.readthedocs.io/en/latest/oauth2_workflow.html#backend-application-flow)

我几乎直接尝试了他们的示例,但运气不佳:

from oauthlib.oauth2 import BackendApplicationClient
from requests_oauthlib import OAuth2Session

ktclient_id = r'my-client-id'
ktclient_secret = r'my-client-secret'
ktredirect_uri = r'http://www.company.com/whatever'

client = BackendApplicationClient(client_id=ktclient_id)
oauth = OAuth2Session(client=client)

token = oauth.fetch_token(token_url=r'https://launchpad.37signals.com/authorization/token',
                        client_id=ktclient_id,
                        client_secret=ktclient_secret)

这是我得到的错误:

Traceback (most recent call last):
  File "./get-token.py", line 20, in <module>
    client_secret=ktclient_secret)
  File "/home/mwilson/.local/lib/python2.7/site-packages/requests_oauthlib/oauth2_session.py", line 244, in fetch_token
    self._client.parse_request_body_response(r.text, scope=self.scope)
  File "/home/mwilson/.local/lib/python2.7/site-packages/oauthlib/oauth2/rfc6749/clients/base.py", line 409, in parse_request_body_response
    self.token = parse_token_response(body, scope=scope)
  File "/home/mwilson/.local/lib/python2.7/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 376, in parse_token_response
    validate_token_parameters(params)
  File "/home/mwilson/.local/lib/python2.7/site-packages/oauthlib/oauth2/rfc6749/parameters.py", line 386, in validate_token_parameters
    raise MissingTokenError(description="Missing access token parameter.")
oauthlib.oauth2.rfc6749.errors.MissingTokenError: (missing_token) Missing access token parameter.

我试过在行中指定 redirect_uri

oauth = OAuth2Session(client=client, redirect_uri=ktredirect_uri)

我得到了相同的结果。有没有人有这方面的运气? Basecamp 3 还需要什么其他参数?

最佳答案

Basecamp 3 仅支持使用 Web 服务器的 Oauth2 身份验证,

您需要按照以下步骤操作:

  1. 使用您的客户端 ID 和客户端密码向 basecamp 发出请求并获取访问代码
  2. 用您收到的访问代码换取访问 token 、刷新 token 和到期时间

您可以继续使用 Flask 或 Django 应用程序。

  1. 您需要在 https://launchpad.37signals.com/integrations 注册您的应用程序

  2. 对于 django,您可以将重定向 url 指定为 localhost:8000/view_name

  3. 执行这些步骤后获得的访问 token 可用于通过 API 发出任何请求。通常访问 token 会持续大约一周或更长时间,直到您过度使用它。

django 应用程序示例: View .py:

def authorize_basecamp(request) :
    return HttpResponseRedirect("https://launchpad.37signals.com/authorization/new?type=web_server&client_id=<your client id>&redirect_uri=<your redirect url>")

以上是将客户端重定向到 basecamp 身份验证站点,他将在该站点登录并通过 basecamp 授权您的 Web 应用

假设指定为重定向的 url 是:http://localhost:8000/app_name/get_token/ 那么获取访问 token 的 View 将是:

def get_token (request) :
    print  (request)
    URL = "https://launchpad.37signals.com/authorization/token"
    # your API key here 
    client_id ="<your_id>"
    client_secret = "<your secret>"
    ver_code = request.GET.get('code')
    redirect_uri = "http://localhost:8000/app_name/get_token/"

    data = {'type':'web_server', 'client_id':client_id, 'client_secret':client_secret, 'redirect_uri':redirect_uri,'code':ver_code } 

    # sending post request and saving response as response object
    print (ver_code, URL) 
    r = requests.post(url = URL, data = data)
    print (r)
    dic = r.json()
    access_token = dic['access_token']
    refresh_token = dic['refresh_token']

    headers = {
        'Authorization': 'Bearer '+access_token,
        'User-Agent': '<your agent_name>',
    }

    response = requests.get('https://launchpad.37signals.com/authorization.json', headers=headers)

    dic2 = response.json()

    expires_at = dic2['expires_at']
    lis_api_urls = []

    for j in dic2['accounts']: 
        lis_api_urls.append(j['href'])

    api_base_url = lis_api_urls[0]

    
    return HttpResponse("Access Token : %s Refresh Token : %s ".format(access_token,refresh_token))

关于python - 如何自动对 basecamp 3 api 进行身份验证(最好在 python 中)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38906233/

相关文章:

oauth-2.0 - 系统上下文中 token 交换后 "act"声明的形状

iphone - ASIHttpRequest 响应的编码问题

ruby-on-rails - 尝试通过 Rails 访问 Basecamp API

python - 带有消费者 key 和 secret 的简单 python oAuth 1.0 示例

python - 词云阿拉伯语

python - 使用 NLTK,如何获得列表的离散图?

python - pytesseract不适用于数字图像

python - 基于 Flask-Stormpath token 的身份验证

oauth-2.0 - 如何在不和谐时从 oauth 获取用户 ID

jquery - 术语 "Backchannel Request"是什么意思以及如何发出反向 channel 请求