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

How can I automate authentication to basecamp 3 api (preferably in python)?

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

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

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

我使用 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. 用您收到的访问代码换取访问令牌、刷新令牌和到期时间

您可以继续使用 flask 或 django 应用程序。

  1. 您需要在 https://launchpad.37signals.com/integrations

  2. 注册您的应用
  3. 对于 django,您可以将重定向 url 指定为 localhost:8000/view_name

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

django 应用示例: views.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/ 那么获取访问令牌的视图将是:

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 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))