【发布时间】:2019-08-03 10:08:19
【问题描述】:
所以我有一段时间出于调试原因在 Python 的 Localhost 上运行我的应用程序,但现在我非常想让我的 Flask 应用程序在我的 上运行Apache 本地主机。我已经在 Vagrant 上配置了 Ubuntu 以在 Apache 上运行该应用程序,而现在唯一不起作用的是 Facebook 登录。虽然我的 Google 登录方法在 Python 的 Localhost 和 Apache 上都可以正常工作,但我的 Facebook 登录方法只能在 Python 的 Localhost 上工作,并且由于某种原因不能在 Apache 的 Localhost 上工作。
具体来说,在执行 fb 登录功能的 fbconnect() 方法 中,当代码到达我的 第 4 次打印( print("4. FB http request results: %s" % result) ) 给定代码块(向下滚动查看代码块),第4次打印发出的消息是这个错误:
4. FB http request results: b'{"error":{"message":"Invalid OAuth access token.","type":"OAuthException","code":190,"fbtrace_id":"AjWGMxq54MULV0sCpjpW2DT"}}'
我不知道 b' 在那里做了什么(它出现在错误消息的开头)以及如何删除它,但它也出现在第一个 print(_print("1. Access token: %s " % access_token)_) 中:
1. Access token: b'EAAE7dgXeMUup...'
并且它不会出现在 2nd 或 3rd 印刷品中:
2. App ID: 3425...
3. App secret: 75a2...
我认为问题是由那些 b' 引起的,因为当我在 Python Localhost 上运行应用程序时,它们没有出现在我的打印中,而且它们也没有出现在 Apache 上的 2nd 或 3rd 打印中,但 我不确定 因为它们可能会出现,因为打印输出在我在“print.log”文件中写出消息,因为 Apache 实际上并不像 Python 的 Localhost 那样将消息打印到终端。 这是我的 fbconnect() 方法:
def fbconnect():
''' Validate state token, by confirming that the token that the client sends
to the server matches the token that the server sent to the client. This
round-trip verification helps ensure that the user is making the request
not a malicious script. Using the request.args.get() method, we examine
the state token passed in by the ajax request and compare with the state
of the login_session. If these 2 do NOT match, we create a response of an
invalid state token and return this message to the client. '''
if request.args.get('state') != login_session['state']:
response = make_response(json.dumps('Invalid state parameter.'), 401)
response.headers['Content-Type'] = 'application/json'
return response
access_token = request.data
print("1. Access token: %s " % access_token)
# Get Facebook app ID.
app_id = json.loads(open('/vagrant/Inhinito/fb_client_secrets.json', 'r').read())['web']['app_id']
print("2. App ID: %s " % app_id)
# Get Facebook app secret.
app_secret = json.loads(open('/vagrant/Inhinito/fb_client_secrets.json', 'r').read())['web']['app_secret']
print("3. App secret: %s " % app_secret)
# Make http request to the Facebook API.
url = "https://graph.facebook.com/oauth/access_token?client_id=%s" % (app_id)
url += "&client_secret=%s&grant_type=fb_exchange_token" % (app_secret)
url += "&fb_exchange_token=%s" % (access_token)
http = httplib2.Http()
result = http.request(url, 'GET')[1]
print("4. FB http request results: %s" % result)
....
这是我从 Python 的 Localhost 获得的正确输出:
1. Access token: EAgE7...
4. FB http request results: {"access_token":"EAgE7...","token_type":"bearer","expires_in":5183999}
【问题讨论】:
-
您确定要使用
request.data——这是请求的“原始”数据,您可能需要request.form["access_token"]或类似的。b表示它的字节数。如果 Python 知道它是如何编码的,它可以将它转回一个字符串——看起来你将它作为 UTF8 发送,所以你可以使用类似access_token.decode('UTF-8')的东西对其进行解码。我相信如果你只是使用request.form["access_token"]它已经知道并处理了解码。 -
@Doobeh 你是对的,这就是问题所在,在你发布的时候发现了什么问题,你在这里回答:stackoverflow.com/questions/6269765/… 如果你使用 request.form["access_token"]它返回的某些原因:
Bad Request The browser (or proxy) sent a request that this server could not understand但使用access_token.decode('UTF-8')有效:/ 我想知道为什么...
标签: python-3.x python-2.7 flask python-requests facebook-access-token