OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How can I using python "get" requests to access API responses with Bearer Token

  • Thread starter Thread starter Han Xiao
  • Start date Start date
H

Han Xiao

Guest
I ran my codes below but get a response of "Error: 401 Status Code: 401; Unauthorized"

def Pods_Assets(token): url = "https://api.aqmeshdata.net/api/Pods/Assets_V1"

Code:
# Implement Bearer authentication
class BearerAuth(requests.auth.AuthBase):
    def __init__(self, bearer_token):
        self.token = bearer_token

    def __call__(self, r):
        r.headers['Authorization'] = 'Bearer ' + self.token
        return r

api_token = {"authorization": token}

auth = BearerAuth(api_token["authorization"])

headers = {"Accept": "application/json"}

# Make the GET request
response = requests.get(url, auth=auth, headers=headers)

if response.status_code == 200:
    json_response = response.json()
    data: object = json_response
    print('Successfully retrieve data: [site_details]')
    print('Response time:', response.elapsed.total_seconds(), 'seconds', '\n')
    return data
else:
    print("Error:", response.status_code, response.text)

I tried to use POST Requests Online tools to figure out the website is still working properly. The raw is listed below.

Code:
GET /api/Pods/Assets_V1 HTTP/1.1
Authorization: Bearer  xxxxx
Host: api.aqmeshdata.net
Accept: application/json

The supplier provided me an example file. The content is listed below.

Code:
        "request": {
            "auth": {
                "type": "bearer",
                "bearer": [
                    {
                        "key": "token",
                        "value": "{{SampleAuthToken}}",
                        "type": "string"
                    }
                ]
            },
            "method": "GET",
            "header": [
                {
                    "key": "Accept",
                    "value": "application/xml",
                    "disabled": true
                },
                {
                    "key": "Accept",
                    "value": "application/json",
                    "disabled": true
                }
            ],
            "url": {
                "raw": "https://api.aqmeshdata.net/api/Pods/Assets",
                "protocol": "https",
                "host": [
                    "api",
                    "aqmeshdata",
                    "net"
                ],
                "path": [
                    "api",
                    "Pods",
                    "Assets"
                ]
            }
        },
<p>I ran my codes below but get a response of "Error: 401 Status Code: 401; Unauthorized"</p>
<p>def Pods_Assets(token):
url = "https://api.aqmeshdata.net/api/Pods/Assets_V1"</p>
<pre><code># Implement Bearer authentication
class BearerAuth(requests.auth.AuthBase):
def __init__(self, bearer_token):
self.token = bearer_token

def __call__(self, r):
r.headers['Authorization'] = 'Bearer ' + self.token
return r

api_token = {"authorization": token}

auth = BearerAuth(api_token["authorization"])

headers = {"Accept": "application/json"}

# Make the GET request
response = requests.get(url, auth=auth, headers=headers)

if response.status_code == 200:
json_response = response.json()
data: object = json_response
print('Successfully retrieve data: [site_details]')
print('Response time:', response.elapsed.total_seconds(), 'seconds', '\n')
return data
else:
print("Error:", response.status_code, response.text)
</code></pre>
<p>I tried to use POST Requests Online tools to figure out the website is still working properly. The raw is listed below.</p>
<pre><code>GET /api/Pods/Assets_V1 HTTP/1.1
Authorization: Bearer xxxxx
Host: api.aqmeshdata.net
Accept: application/json
</code></pre>
<p>The supplier provided me an example file. The content is listed below.</p>
<pre><code> "request": {
"auth": {
"type": "bearer",
"bearer": [
{
"key": "token",
"value": "{{SampleAuthToken}}",
"type": "string"
}
]
},
"method": "GET",
"header": [
{
"key": "Accept",
"value": "application/xml",
"disabled": true
},
{
"key": "Accept",
"value": "application/json",
"disabled": true
}
],
"url": {
"raw": "https://api.aqmeshdata.net/api/Pods/Assets",
"protocol": "https",
"host": [
"api",
"aqmeshdata",
"net"
],
"path": [
"api",
"Pods",
"Assets"
]
}
},
</code></pre>
 

Latest posts

B
Replies
0
Views
1
Blundering Ecologist
B
Top