python-3.x - Flask - b' 文本出现在 request.data 结果之前

标签 python-3.x python-2.7 flask python-requests facebook-access-token

所以我有一段时间在 上运行我的应用程序。 Python的本地主机出于调试原因,但现在我非常想制作我的 flask 应用程序在我的 上运行Apache 本地主机 .我已经在 Vagrant 上配置了 Ubuntu 以在 Apache 上运行该应用程序,而现在唯一不起作用的是 Facebook 登录。虽然我的 Google 登录方法在 Python 的 Localhost 和 Apache 上都可以正常工作,但我的 Facebook 登录方法 出于某种原因,仅适用于 Python 的本地主机,而不适用于 Apache 的本地主机 .

具体来说,在我的 fbconnect() 方法 当代码到达我的 时,执行 fb 登录功能第 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("1. Access token: %s "% access_token)_) :
1. Access token: b'EAAE7dgXeMUup...'

不出现 第二第三打印:
2. App ID: 3425...
3. App secret: 75a2...

我认为问题是由那些 引起的b' 因为当我在我的 上运行应用程序时,它们不会出现在我的打印中Python 本地主机 而且它们也没有出现在 中第二第三在 Apache 上打印,但 我不确定因为它们可能会出现,因为当我在“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}

最佳答案

正如 Doobeh 在评论中提到的,解决方案是将客户端发送的数据解码为 UTF-8。这是 Python2 与 Python3 问题详细解释here .

b' ' 显然是用来表示字符串是二进制 ,而不是 Unicode。
解决方案是使用access_token = request.data.decode('UTF-8')代替access_token = request.datahttp.request(url, 'GET')[1].decode('UTF-8')而不是 http.request(url, 'GET')[1] .

关于python-3.x - Flask - b' 文本出现在 request.data 结果之前,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57337321/

相关文章:

logging - Flask 日志记录配置问题

python - 如何在列表长度达到一定限制后停止追加值?

python-3.x - 解决Pandas数据框中带引号分号的csv文件

python - 在嵌套字典中搜索关键树

python - matplotlib 内联 Python 2.7 %

python - Python Flask 上有单例模式吗?

python - 如何将 UDP 与 Asyncio 一起用于从服务器到客户端的多个文件传输 - python

python - 如何在Python中执行Fortran程序

python-2.7 - 升级到 Python 2.7.10 : Target WSGI script cannot be loaded as Python module

python - 导入错误 : No module named bcrypt