簡體   English   中英

如何自動對 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)

我得到了同樣的結果。 有人對此有任何運氣嗎? 大本營 3 還需要哪些其他參數?

Basecamp 3 僅支持使用 Web 服務器進行 Oauth2 身份驗證,

您需要執行以下步驟:

  1. 使用您的客戶端 ID 和客戶端密碼向 basecamp 發出請求並獲取訪問代碼
  2. 用您收到的訪問代碼換取訪問令牌、刷新令牌和到期時間

您可以繼續使用燒瓶或 django 應用程序。

  1. 您需要在https://launchpad.37signals.com/integrations注冊您的應用

  2. 對於 django,您可以將重定向 url 指定為 localhost:8000/view_name

  3. 按照這些步驟獲得的訪問令牌可用於通過 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 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))

請參閱下面線程中的評論: Basecamp 3 API 的令牌 URL 問題

請注意,您將需要一個 webhook。 無法從您的終端執行所有操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM