403-授权错误-OAuth2.0-访问​​令牌-Azure Api For Fhir

时间:2020-04-20 12:53:55

标签: azure hl7-fhir fhir-server-for-azure

我已使用此链接-https://docs.microsoft.com/en-gb/azure/healthcare-apis/tutorial-web-app-fhir-server

为FHIR部署和配置了Azure API。

使用邮递员,我能够成功地将患者信息插入fhir-server。

要使其自动化,我正在使用python和客户端服务流。

   def get_access_token(self):

        token_url = 'https://login.microsoftonline.com/{}/oauth2/v2.0/token'.format(azure_app_tenant_id)

        token_data = {
        'grant_type': 'client_credentials',
        'client_id': azure_app_client_id,
        'client_secret': azure_app_client_secret,
        'scope': fhir_endpoint_url + "/.default",

        }

        token_r = requests.post(token_url, data=token_data)

        log.info("Retrieving Access Token")
        if token_r.status_code == 200:
            log.info("Access Token Retrieved Successfully")
        else:
            raise Exception("Error retrieving access token")

        print(token_r.json()["access_token"])
        return token_r.json()["access_token"]

我能够使用get_access_token获取访问令牌。但是,当我使用access_token并插入患者记录时,其抛出授权失败-403错误。

    def insert_patient_record(self, payload):
        log.info("Inserting Patient Record")
        headers = {
            'Authorization': 'Bearer {}'.format(self.get_access_token()),
            'Content-Type': 'application/json'
        }

        response = requests.request("POST", fhir_endpoint_url, headers=headers, data=payload)
        print("Response Code: ", response.status_code)
        if response.status_code == 200:
            log.info("Patient Record inserted Successfully")
        else:
            print("Response Text: ", response.text)
            raise Exception("Error inserting patient record")

Response Text:  {"resourceType":"OperationOutcome","id":"24515888da8e954da1e763d96193155b","issue":[{"severity":"error","code":"forbidden","diagnostics":"Authorization failed."}]}

注意:在“ FHIR服务器身份验证”部分中,我添加了我之前在ADD中创建的已注册APP的对象ID。

1 个答案:

答案 0 :(得分:1)

似乎您没有 not 添加了已注册应用程序的(正确)对象ID。重要的是,应用程序注册具有对象ID,但是服务主体也是如此。它是您要查找的服务主体的应用程序ID。

在此处查看说明:

https://docs.microsoft.com/en-us/azure/healthcare-apis/find-identity-object-ids

您可以使用PowerShell找到它的服务主体对象ID:

$(Get-AzureADServicePrincipal -Filter "AppId eq 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX'").ObjectId

或Azure CLI:

az ad sp show --id XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX | jq -r .objectId

我还建议您将令牌粘贴到类似https://jwt.ms的地方,然后查看oid声明。那是您添加的对象ID吗?

相关问题