当我在EC2 Linux服务器实例(运行Ubuntu 13.04)上运行它时,我正在努力获得OAuth2授权来处理我正在处理的脚本。相关摘录是:
with open('creds.txt') as f:
creds = {}
for line in f:
creds[line.split(',')[0]] = line.split(',')[1].rstrip('\n')
self.client_id = creds['client_id']
self.client_secret = creds['client_secret']
self.username = creds['username']
self.password = creds['password'])
token_response = requests.post(
"https://example.com/oauth2/access_token/",
data={
"grant_type": "password",
"client_id": self.client_id,
"client_secret": self.client_secret,
"username": self.username,
"password": self.password,
"scope": "read+write"}).json()
它在我的家用计算机上运行正常(运行Windows 7),当我尝试远程运行它时,我没有:{u'error': u'invalid_client'}
。
我已尝试设置新的客户端ID和密码,但仍会得到相同的响应。
CURL
成功进行身份验证,我消除了这种可能性。我现在唯一能想到的是,请求库可能在Ubuntu上以不同的方式处理POST请求。有谁知道是否是这种情况?
答案 0 :(得分:0)
当Nix和Windows环境之间存在差异时,这可能是我应该想到的第一件事:
始终检查EOL字符!
问题是当从我的凭证文件中获取用户名,密码等时,我正在使用string.rstrip('\n')
剥离换行符,因此在离开\r
回车的Unix环境中后面的字符然后作为POST
请求的一部分传递。
在两种环境中都有效的simple and correct solution是使用string.rstrip()
来删除所有尾随空格和行尾字符。