如何自动验证basecamp 3 api(最好是python)?

如何自动验证basecamp 3 api(最好是python)?,python,oauth2,basecamp,Python,Oauth2,Basecamp,这是我第一次进入OAuth,所以如果我偏离了基准,或者走错了方向,请耐心等待 我想写一个脚本,从basecamp 3中提取信息,格式化,然后通过电子邮件发送。我已经在basecamp 2上这样做了,效果很好。但是basecamp 3不允许简单的身份验证。这只是一个每周通过cron运行一次的脚本 我发现的大多数OAuth示例都需要获取授权url、在浏览器中访问该url、授予授权等才能获取访问令牌。请告诉我有一种自动方式!我不能让这变成一个手工过程 我使用oauthlib请求尝试了后端应用程序流(在

这是我第一次进入OAuth,所以如果我偏离了基准,或者走错了方向,请耐心等待

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

我发现的大多数OAuth示例都需要获取授权url、在浏览器中访问该url、授予授权等才能获取访问令牌。请告诉我有一种自动方式!我不能让这变成一个手工过程

我使用oauthlib请求尝试了后端应用程序流(在此处找到:)

我已经很直接地尝试了他们的例子,但没有运气:

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.

我得到了同样的结果。有人有过这样的运气吗?basecamp 3还需要什么其他参数?

请参阅下面线程中的我的评论:


请注意,您将需要一个webhook。无法从终端执行所有操作。

Basecamp 3仅支持使用web服务器进行Oauth2身份验证

您需要遵循以下步骤:

  • 使用您的客户id和客户机密向basecamp发出请求,并获取访问代码
  • 用收到的访问代码换取访问令牌、刷新令牌和到期时间
  • 您可以继续使用flask或django应用程序进行相同的操作

  • 您需要在注册应用程序

  • 对于django,可以将重定向url指定为localhost:8000/view\u name

  • 在执行这些步骤后获得的访问令牌可用于通过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>")
    
    def授权_基地(请求):
    返回HttpResponseRedirect(“https://launchpad.37signals.com/authorization/new?type=web_server&client_id=&redirect_uri=")
    
    上面是将客户端重定向到basecamp身份验证站点,在那里他将通过basecamp登录并授权您的web应用程序

    假设指定为重定向的url为: 然后,获取访问令牌的视图将是:

    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))
    
    def get_令牌(请求):
    打印(请求)
    URL=”https://launchpad.37signals.com/authorization/token"
    #这里是您的API密钥
    客户_id=“”
    client_secret=“”
    ver_code=request.GET.GET('code'))
    重定向_uri=”http://localhost:8000/app_name/get_token/"
    数据={'type':'web\u server','client\u id':client\u id,'client\u secret':client\u secret,'redirect\u uri':redirect\u uri,'code':ver\u code}
    #发送post请求并将响应保存为响应对象
    打印(版本代码、URL)
    r=requests.post(url=url,data=data)
    印刷品(r)
    dic=r.json()
    访问令牌=dic[“访问令牌”]
    刷新令牌=dic[“刷新令牌”]
    标题={
    “授权”:“承载人”+访问令牌,
    “用户代理”:“,
    }
    response=requests.get('https://launchpad.37signals.com/authorization.json,headers=headers)
    dic2=response.json()
    expires_at=dic2['expires_at']
    lis_api_URL=[]
    对于j in账户:
    lis_api_url.append(j['href'])
    api_base_url=lis_api_url[0]
    返回HttpResponse(“访问令牌:%s刷新令牌:%s”。格式(访问令牌,刷新令牌))
    
    也许您最好也在此处提供一些详细信息。谢谢您的详细回复!但不幸的是,我不再使用basecamp了。不客气,我注意到了你的问题,甚至当我被basecamp oauth2困住的时候,现在我已经实现了我想发布的答案
    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))