这是我第一次进入OAuth,所以如果我离基地很远,或者走错了方向,请耐心地对待我。
我想写一个脚本,从basecamp 3中提取信息,格式化它,并在电子邮件中发送它。我已经用basecamp 2做了,它工作的很好。但是basecamp 3不允许简单的身份验证。这只是一个脚本,通过cron运行每周一次。
我发现的大多数OAuth示例都需要获得授权url、在浏览器中访问它、授予授权等等,才能获得访问令牌。请告诉我有一种自动的方式!我不能让这变成一个人工过程。
我使用请求-oauthlib尝试了后端应用程序流(见此处: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还需要什么参数?
发布于 2019-06-13 07:43:55
Basecamp 3只支持web服务器的Oauth2身份验证,
您需要遵循以下步骤:
您可以继续一个烧瓶或django应用程序的相同。
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是: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))
发布于 2019-02-02 04:24:56
请参阅下面的帖子:Problem with Token URL for Basecamp 3 API
请注意,您将需要一个网络钩子。不可能从你的终端机做所有的事。
https://stackoverflow.com/questions/38906233
复制相似问题