是否有python脚本来生成暴雪API访问令牌?

2024-09-28 03:24:55 发布

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

我对编码相当陌生,但我正在尝试一个从暴雪api中提取一些数据的机器人。我拥有的bot运行良好,但每次访问令牌更改时,我都必须进入并手动更改json数据的url。我知道令牌每24小时都会更改一次,但我无法想象开发人员每天都要不断地到控制台上用更新的访问令牌调出新的url。我一直在浏览暴雪OAuth文档,发现有人为python发布了以下代码:

import requests
import json
from requests.auth import HTTPBasicAuth


def create_access_token(client_id, client_secret, region = 'us'):
    url = "https://%s.battle.net/oauth/token" % region
    body = {"grant_type": 'client_credentials'}
    auth = HTTPBasicAuth(client_id, client_secret)
    response = requests.post(url, data=body, auth=auth)
    return response.json()
create_access_token()

但是当我把我的客户id和客户机密传递给这个时,我没有得到任何信息。任何帮助都会很棒。谢谢


Tags: 数据importclienttokenauthidjsonurl
1条回答
网友
1楼 · 发布于 2024-09-28 03:24:55

您可以使用类似this的在线CURL to Python Requests ansible工具,从client credential flow的文档CURL示例中获取正确的请求格式:

卷曲

curl -u {client_id}:{client_secret} -d grant_type=client_credentials https://us.battle.net/oauth/token

Python

import requests

def create_access_token(client_id, client_secret, region = 'us'):
    data = { 'grant_type': 'client_credentials' }
    response = requests.post('https://%s.battle.net/oauth/token' % region, data=data, auth=(client_id, client_secret))
    return response.json()

response = create_access_token(YOUR_CLIENT_ID, YOUR_CLIENT_SECRET)
print(response)

相关问题 更多 >

    热门问题