【问题标题】:Microsoft partner generate token微软合作伙伴生成令牌
【发布时间】:2021-08-23 18:37:46
【问题描述】:

我使用 python 做到了这一点。但无法生成有助于获取客户数据的代币。 获取访问令牌的代码

url = "https://login.microsoftonline.com/"+tanat_id+"/oauth2/token"
headers = {
    "Accept": "application/json",
    "return-client-request-id": "true",
    "Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
    "Host": "login.microsoftonline.com",
    "Content-Length": "194",
    "Expect": "100-continue"
}
data = {
    'client_id': client_id,
    'resource': 'https://api.partner.microsoft.com',
    'client_secret': client_secret,
    'grant_type': 'client_credentials',
}

res = requests.post(url, headers=headers, data=data)
access_token = json.loads(res.text)["access_token"]

生成令牌的代码

url = "https://api.partnercenter.microsoft.com/generatetoken"
headers = {
    "Authorization": "Bearer " + str(access_token),
    "Accept": "application/json",
    "content-type": "application/json;charset=UTF-8"
}
data = {
    'grant_type': 'authorization_code',
}

res = requests.post(url, headers=headers, data={"grant_type":"jwt_token"})

但是

>>> res.text
'{"error":"invalid_grant","error_description":"Invalid token: tokenValidationResult == null - True, tokenValidationResult.Principal == null - True, tokenValidationResult.Principal.Identity == null- True, tokenValidationResult.Principal.Identity.IsAuthenticated - "}'

如果我尝试获取客户列表,则使用 access_token 会返回 401 错误。

#Customer List

url = "https://api.partnercenter.microsoft.com/v1/customers?size=40"
headers = {
    "Authorization": "Bearer " + str(access_token),
    "Accept": "application/json",
}

res = requests.get(url, headers=headers)
res
<Response [401]>
res.text
""

【问题讨论】:

    标签: python azure api token


    【解决方案1】:

    第一个错误 "error": "invalid_grant" 是因为权限问题,这通常发生在长时间刷新凭据或尝试以下步骤:

    • 选择“Azure:登录”

    • 删除了关联的帐户。

    • 重新添加帐户。

    401 is Unauthorized 错误:此错误通常表示 HTTP 身份验证请求标头中可能缺少访问令牌或令牌无效或已过期

    对于这些常见错误,请参阅document

    【讨论】:

      【解决方案2】:

      您需要从https://graph.windows.net获取第一个访问令牌

      data = {
          'client_id': client_id,
          'resource': 'https://graph.windows.net',
          'client_secret': client_secret,
          'grant_type': 'client_credentials',
      }
      

      请检查请求标头here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-17
        • 2013-04-26
        • 1970-01-01
        • 2022-06-18
        • 1970-01-01
        相关资源
        最近更新 更多