简体   繁体   English

如何使用python作为客户端使Trello会话持久化?

[英]How to make a Trello session persist using Python as the client?

I'm trying to write a Python script that, from my command line, will allow me to: 我正在尝试编写Python脚本,该脚本将从命令行允许我执行以下操作:

  1. Log in to Trello 登录到Trello

  2. Use the Trello Python API to retrieve data from Trello boards, etc. 使用Trello Python API从Trello板等检索数据。

I'm restricted to using Python 2.7.6, so please don't offer any Python 3 suggestions — I won't be able to use them. 我被限制使用Python 2.7.6,所以请不要提供任何Python 3建议-我将无法使用它们。

I'm able to log in to Trello using the requests module; 我可以使用请求模块登录Trello; however, I'm stuck on retrieving a user key in order to be able to use the Trello API. 但是,我坚持要检索用户密钥才能使用Trello API。

Here's what I'm doing so far: 到目前为止,这是我正在做的事情:

import trello
import requests

# Not shown: retrieve username and password from user

sess = requests.Session()

loginData = {
    'method': 'password',
    'factors[user]': username,
    'factors[password]': password
}

TRELLO_LOGIN_URL = "https://trello.com/1/authentication"

resp = sess.post(TRELLO_LOGIN_URL, trelloLogin)     # resp => <Response [200]>

At this point, the resp.text consists of a dictionary (which works equally well in JavaScript or Python) containing a single key code whose value is a very long string of gibberish). 此时, resp.text由一个字典(在JavaScript或Python中同样有效)组成,该字典包含单个键code其值是一个很长的乱码。 I assume this (and the 200 status code in the response object) means that I logged in successfully. 我假设这(以及响应对象中的200状态代码)表示我已成功登录。

So, now that I'm (presumably) logged in, I request the token URL from the API: 因此,现在(大概)已经登录,我从API请求令牌URL:

api = trello.TrelloApi(MY_API_KEY)
tokenUrl = api.get_token_url('MyApp') # => https://trello.com/1/authorize?key=<mykey>&name=MyApp&expiration=30days&response_type=token&scope=read,write

But when I try to use that URL: 但是当我尝试使用该URL时:

sess.get(tokenUrl)

the response text I get (consisting of the HTML for an authorization page) has a "Log In" button, rather than the "Approve" button that I see when I do this in my browser. 得到的响应文本(由授权页面的HTML组成)具有“登录”按钮,而不是在浏览器中看到的“批准”按钮。 To me, this suggests that the requests.Session object is not persisting properly. 对我来说,这表明requests.Session对象不能正确持久化。 Perhaps it's using incorrect cookies or something in the get() request? 也许它使用了不正确的Cookie或get()请求中的内容?

Honestly, I'm not all that concerned about the nuts and bolts of what's going wrong, only about what I have to do to fix it. 老实说,我并不是只关心发生问题的细节,而只关心我必须解决的问题。 How do I make the session persist into that get() call so I can do the next part? 如何使会话持久到该get()调用中,以便可以进行下一部分?

(Of course, it's entirely possible — even likely — that my approach is incorrect from the ground up, but I haven't been able to find any documentation to tell me what I should be doing differently, so this is the best I could come up with from what I have found. If there are instructions on doing this another way — but still from the command line — feel free to point me to them.) (当然,从根本上来说,我的方法完全有可能是错误的,甚至是可能的,但是我一直找不到任何文档来告诉我应该做些什么,所以这是我能做到的最好了从我发现如果有做另一种方式的指令-但仍然在命令行-随意点我给他们)

Apparently there are 2 calls to make to get the token: 显然有两个调用来获取令牌:

  1. https://trello.com/1/authentication which contains the code string in the response https://trello.com/1/authentication ,其中包含响应中的code字符串
  2. https://trello.com/1/authorization/session use the code retrieved previously to post, should return 204 and then token would be set to cookies. https://trello.com/1/authorization/session使用先前获取的code进行发布,应返回204,然后将token设置为cookie。 (You will also need to provide a dsc string, for the detail please try inspect the requests via the chrome dev tools.) (您还需要提供dsc字符串,有关详细信息,请尝试通过chrome dev工具检查请求。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM