【问题标题】:How to fetch values from bytes?如何从字节中获取值?
【发布时间】:2019-10-04 07:18:00
【问题描述】:

在 POST 请求之后,我得到一个以字节为单位的响应,但我想获取我的 access_token、refresh_token 怎么可能。

payload = 'grant_type=authorization_code&code=' + self.response_code + '&redirect_uri=' + self.redirect_uri
        auth = self.client_id+':'+self.client_secret
        endcoded_u = base64.b64encode(auth.encode("ascii")).decode("ascii")
        response = requests.post(
            url='https://api.getbase.com/oauth2/token',
            headers={
                'Content-Type': 'application/x-www-form-urlencoded',
                'Authorization': 'Basic %s' % endcoded_u,
            },
            data=payload,
            verify=True
        )
b'{"access_token":"5716f50fead975aa81340757cadbb1a2154681d9750c53abe4672143c7d938c3","token_type":"bearer","expires_in":3600,"refresh_token":"365e14fbd4d0e6a25486bf11cea3ebe84dbf5f2485fd95443a579c04f75e5e6e","scope":"read write profile sync"}'

有什么帮助吗?

【问题讨论】:

  • 你能粘贴代码吗?可能你没看懂
  • 你从哪里得到这个字符串? response ?如果是,那么response.json() 应该直接给你dict 然后你可以直接访问access_token
  • 我不知道为什么人们会贬低我的问题:(

标签: python post https byte response


【解决方案1】:

你可以试试:

import json

data = b'{"access_token":"5716f50fead975aa81340757cadbb1a2154681d9750c53abe4672143c7d938c3","token_type":"bearer","expires_in":3600,"refresh_token":"365e14fbd4d0e6a25486bf11cea3ebe84dbf5f2485fd95443a579c04f75e5e6e","scope":"read write profile sync"}'
data = json.loads(data.decode())

print(data.get("access_token"))
# '5716f50fead975aa81340757cadbb1a2154681d9750c53abe4672143c7d938c3'

【讨论】:

    【解决方案2】:

    import json
    token_bytes=b'{"access_token":"5716f50fead975aa81340757cadbb1a2154681d9750c53abe4672143c7d938c3","token_type":"bearer","expires_in":3600,"refresh_token":"365e14fbd4d0e6a25486bf11cea3ebe84dbf5f2485fd95443a579c04f75e5e6e","scope":"read write profile sync"}'
    
    token_byte_string=token_bytes.decode("utf-8") #decode bytes to string
    
    response = json.loads(token_byte_string) # built a dict from above string
    
    response["access_token"] # this is your access token
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 2012-03-10
      相关资源
      最近更新 更多