【问题标题】:Why does OAuth2 authentication work on home machine but not on a server?为什么 OAuth2 身份验证在家用机器上有效,但在服务器上无效?
【发布时间】:2013-07-22 08:05:22
【问题描述】:

当我在 EC2 Linux 服务器实例(运行 Ubuntu 13.04)上运行我正在处理的脚本时,我正在努力获得 OAuth2 授权。相关的sn-p是:

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 请求的方式不同。有人知道是不是这样吗?

【问题讨论】:

  • This answer 似乎是相关的,因为它表明客户端 ID 关心它是在什么操作系统上生成的。不过,我在the docs 中找不到任何可以确认的信息。

标签: python python-2.7 oauth-2.0 eol


【解决方案1】:

当 Nix 和 Windows 环境之间存在差异时,这可能是我首先应该想到的:

始终检查 EOL 字符!

问题是,当从我的凭据文件中获取用户名、密码等时,我使用 string.rstrip('\n') 剥离换行符,因此在留下 \r 回车符后面的 Unix 环境中是然后作为POST 请求的一部分传递。

在两种环境中都可以使用的simple and correct solution 是使用string.rstrip(),它会去除所有尾随空格和行尾字符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    • 2015-07-07
    • 2018-04-02
    • 2023-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多