[英]Why does OAuth2 authentication work on home machine but not on a server?
當我在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請求。 有誰知道是這樣嗎?
當Nix和Windows環境之間存在差異時,這可能是我應該想到的第一件事:
始終檢查EOL字符!
問題是,當從我的憑證文件中獲取用戶名,密碼等時,我使用string.rstrip('\\n')
剝離了換行符,因此在Unix環境中留下了\\r
回車符然后作為POST
請求的一部分傳遞。
在兩種環境中都可以使用的簡單正確的解決方案是使用string.rstrip()
,該方法將string.rstrip()
所有尾隨空白和行字符的末尾。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.