Microsoft Graph API同步API,更新密钥令牌仅在第二次调用时有效

2024-05-19 03:02:36 发布

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

我正在实现(用Python和Microsoft Graph API)基于AWS模板创建Azure AD应用程序。我在实现自动角色设置时遇到了困难,如本文档中所述:https://docs.microsoft.com/fr-fr/graph/application-provisioning-configure-api?tabs=http#step-3-authorize-access

当我在创建同步作业之后第一次调用servicePrincipals/{id}/synchronization/secretsAPI时,我收到一个HTTP错误(400-错误请求),其正文如下:

{
  "error": {
    "code": "BadRequest",
    "message": "The credentials could not be saved. This is due to an internal storage issue in the Microsoft Azure AD service. For information on how to address this issue, please refer to https://go.microsoft.com/fwlink/?linkid=867915",
    "innerError": {
      "code": "CredentialStorageBadRequest",
      "details": [],
      "message": "The credentials could not be saved. This is due to an internal storage issue in the Microsoft Azure AD service. For information on how to address this issue, please refer to https://go.microsoft.com/fwlink/?linkid=867915",
      "target": null,
      "innerError": {
        "code": "CredentialStorageBadRequest",
        "details": [],
        "message": "Message:The credentials could not be saved. This is due to an internal storage issue in the Microsoft Azure AD service. For information on how to address this issue, please refer to https://go.microsoft.com/fwlink/?linkid=867915",
        "target": null
      },
      "date": "2021-01-05T15:53:59",
      "request-id": "---",
      "client-request-id": "---"
    }
  }
}

当一个用户执行第二个相同的调用(使用MS Graph Explorer、Postman或直接使用Python)时,它可以工作,第二个调用将返回一个与预期类似的HTTP 204!所以我认为我的要求是正确的

这是我的实现(因为我第二次重试该调用,所以可以工作…):

# Default value :
GRAPH_API_URL = "https://graph.microsoft.com/beta/{endpoint}"

class Azure:
  # […]

  # self._http_headers contains my token to access to MS Graph API
  # self._aws_key_id and self._aws_access_key contains AWS credentials

  def _save_sync_job_auth(self, principal_id):
    self._put(
        f"servicePrincipals/{principal_id}/synchronization/secrets",
        {"value": [
            {"key": "ClientSecret", "value": self._aws_key_id},
            {"key": "SecretToken", "value": self._aws_access_key},
        ]},
        retry=1  # If I put 0 here, my script fail
    )

  # […]
  
  def _put(self, endpoint, json, retry=0):
    return self._http_request(requests.put, endpoint, retry, json=json)

  # […]

  def _http_request(self, func, endpoint, retry=0, **kwargs):
    url = GRAPH_API_URL.format(endpoint=endpoint)
    response = func(url, headers=self._http_headers, **kwargs)

    try:
        response.raise_for_status()
    except requests.HTTPError as e:
        if retry:
            logging.warning(f"Error when calling {func.__name__.upper()} {url}")
            return self._http_request(func, endpoint, retry - 1, **kwargs)
        else:
            raise e

    return response

我错过什么了吗?您是否有一个解决方案来删除此“重试黑客”


Tags: tokeyhttpsselfcomidhttprequest

热门问题