我已经部署并配置了Azure API for FHIR,使用此链接--。https:/docs.microsoft.comen-gbazurehealthcare-apistutorial-web-app-fhir-server。
使用postman,我能够成功地将一个病人信息插入到fhir服务器中。
为了实现自动化,我使用了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并插入病人记录时,它抛出了Authorization Failed - 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。
解决方案:
看起来你有 不 添加了注册应用程序的(正确的)对象id。重要的是,应用程序注册有一个对象id,但服务委托人也有。你要找的是服务委托人的申请id。
请参考这里的说明。
https:/docs.microsoft.comen-usazurehealthcare-apisfind-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吗?
未经允许不得转载:编程自学网 » 403 - 授权错误 - OAuth2.0 - 访问令牌 - Azure Api For Fhir