如何从字节中获取值?

2024-10-04 09:18:45 发布

您现在位置:Python中文网/ 问答频道 /正文

在POST请求之后,我得到了一个以字节为单位的响应,但是我想获取我的访问令牌,刷新令牌

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"}'

有什么帮助吗


Tags: selfclienttokenauth字节responsetypeascii
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

您可以尝试:

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'

相关问题 更多 >