我正在尝试为我的工作应用程序获取访问令牌,但我得到“未找到请求的资源”。错误消息

2024-05-13 20:54:55 发布

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

我正在向PingOne API发出POST请求(根据它们的guide),以便检索我的应用程序访问令牌。使用“客户端凭据”作为授权类型。 我为请求提供了所有必需的参数,但仍然得到以下错误:

> /usr/local/bin/python3.7 "/Users/iaburmaileh/Desktop/APIs requests to PingOne/POST_GET_a_Worker_Application_Access_Token.py"
b'\n{       \n\t"id":"77F5305F-9961-4F8F-B8F7-E0E5FDA4FE41",\n\t"code": "NOT_FOUND",\n\t"message":"The requested resource was not found."\n}\n'
Process finished with exit code 0

这是我的代码(**我的env和app用于演示和测试,所以不用担心共享它们的变量)

import requests

apiPath = 'api.pingone.com/v1'
envID = '5bb98115-61c7-4964-96bf-4d2c3a34756b'
appID = 'db04865b-9ab0-448e-a89a-f3cb473ddc7f'
appSecret = 'c_5XTOw6Sh-ei6cHLhvoneoVn-86t.zY1Df7YWcIUfpkN4gTjp1A7QjWUwlEpofp'

url = f"https://{apiPath}/{envID}/as/token"

payload = 'grant_type=client_credentials'
headers = {
   'Content-Type': 'application/x-www-form-urlencoded',
   'Authorization': f'Basic {appID}:{appSecret}'
}


response = requests.request("POST", url, headers=headers, data = payload)

print(response.text.encode('utf8'))

你知道怎么解决这个问题吗?提前感谢:)


Tags: api应用程序urlresponsecodepostrequestsappid
1条回答
网友
1楼 · 发布于 2024-05-13 20:54:55

我在尝试这篇文章时得到了积极的结果:

(编辑:我想我把GET结果放在了前面。)

为了可读性,我将有效负载结构切换到键值集合

authPath = f'https://auth.pingone.com'
envID = '5bb98115-61c7-4964-96bf-4d2c3a34756b'
appID = 'db04865b-9ab0-448e-a89a-f3cb473ddc7f'
url = f"{authPath}/{envID}/as/authorize"

payload = {
        "response_type":"code",
        "client_id":f"{appID}",
        "redirect_uri":"https://example.com",
        "scope":"openid profile p1:read:user",
    }

headers = {
   'Content-Type': 'application/x-www-form-urlencoded'
}

response = requests.request("POST",
                            url,
                            headers = headers,
                            data = payload
                            )

在响应时检查状态代码:

In [19]: response.status_code
Out[19]: 200

以及HTML:

In [21]: print(response.text)

输出:

<!doctype html>

<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta charset="utf-8">
    <link rel="icon" href="data:,">
  <link href="styles.css" rel="stylesheet"></head>
  <body>
    <div id="app" class="app-container" style="display: none;">
      <div class="page brand-background">
        <div class="page__content" id="page-ui-container"></div>
        <div class="page__footer brand-footer-color">
          <!  © Copyright 2003-2020 Ping Identity. All rights reserved.  >
        </div>
      </div>
    </div>
<script type="text/javascript" src="index.bundle.js"></script><script type="text/javascript" src="styles.bundle.js"></script></body>
</html>

相关问题 更多 >