Python request headers. error authenticating with JSON WEB TOKEN - python

I'm learning python by building a simple trading bot. I receive this error while trying to authenticate using a JWT
{
"error": {
"status": 403,
"message": "Authentication credentials were not provided."
}
}
following the example here https://docs.ledgerx.com/reference/tradedcontracts
import requests
url = "https://api.ledgerx.com/trading/contracts/traded"
headers = {
"Accept": "application/json",
"Authorization": "MY_API_KEY"
}
response = requests.get(url, headers=headers)
print(response.text)
for now im inserting the string literal later i will store this value in an .env
thanks for taking the time to read

Can you try this please
import requests
url = "https://api.ledgerx.com/trading/contracts/traded"
headers = {
"Accept": "application/json",
"Authorization": "JWT MY_API_KEY"
}
response = requests.get(url, headers=headers)
print(response.text)

Related

Full authentication is required to access this resource error in python when I am trying to access a url using python requests

This is a python script to make a post call with respective header and body, but I am getting error Full authentication is required to access this resource
import requests
from requests.auth import HTTPBasicAuth
url = 'https://localhost:8000/bucket/items'
body = {
"serialNumbers": [
"AASD-ASD"
]
}
bearer_token = "07eb4900-016f-4523-a9b7-e5d6a87a7f8e"
jwt_token = "jhbasdfiunnX.faf-"
headers = {'content-type': 'application/json',
"authentication":"bearer "+ '07eb4900-a9b7-e5d6a87a7f8e',
"cookie":"jwt="+jwt_token}
headers = {"Cookie":"JWT="+jwt_token, "Authentication":"Bearer "+bearer_token, "Content-Type": "application/json" }
response = requests.post(url, body, auth=HTTPBasicAuth("abcd", "password"),headers=headers).json()
print(response)

using grequest for many request

I'm using the grequest library for translating api requests, I need to quickly send about 2000 requests, but in this case I get an error: response 429, too many requests. How is it possible to set a delay between sending requests in this library?
def translate(array):
config.read("config.ini")
url = "https://translo.p.rapidapi.com/api/v3/translate"
headers = {
"User-agent": "bot 0.1",
"Retry-after": "1",
"content-type": "application/x-www-form-urlencoded",
"X-RapidAPI-Key": config['user']['api_key'],
"X-RapidAPI-Host": "translo.p.rapidapi.com"
}
response = (grequests.request("POST", url, data=payload, headers=headers) for payload in array)
resp = grequests.map(response)
logger.info(f"{resp}")
return resp
array = notes to translate
I try setup "Retry-After", but it doesn't work.

My youtube API integration says unathorized what is wrong with my code?

My code says unauthorized, what am I missing?
When I push the code I get <Response [401]> when I tried creating a json I got error unauthorized.
import requests
from secret import api_key
url = "https://www.googleapis.com/youtube/v3/search"
# channelId= "UCWv7vMbMWH4-V0ZXdmDpPBA"
headers = {
"Authorization": "Bearer" + api_key
}
params = {
"part":"snippet",
"forDeveloper":True,
"channelId": "UCWv7vMbMWH4-V0ZXdmDpPBA",
"maxResults": 49,
"order": "title",
"q":"python"
}
response = requests.get(url, headers=headers,params=params)
print(response)

Python Discord OAuth2 - Bad Request (Guild.Join)

Hi i'm trying to add a user to my guild using Discord's API.
The Scope I'm using is Guild.Join, Identify and Guilds. identify%20guilds%20guilds.join
The URL Is https://discordapp.com/api.
Here's my code:
#staticmethod
def add_to_guild(access_token, userID):
url = f"https://discordapp.com/api/guilds/{guildId that I cant show}/members/{userID}"
botToken = "<Bot Token I can't SHow haha>"
headers = {
"Authorization" : f"Bot {botToken}",
'Content-Type': 'application/json'
}
payload = {
'access_token' : access_token
}
response = requests.put(url=url, data=payload, headers=headers)
print(response.text)
when I call this method, I receive this error:
{"message": "400: Bad Request", "code": 0}
I've been through the Discord's Documentation countless of times and searched the internet to no avail.
Can someone please help ? Thanks.
you had to replace data to json
def add_to_guild(access_token, userID):
print(userID)
url = f"{Oauth.discord_api_url}/guilds/<GuildID>/members/{userID}"
headers = {
"Authorization" : "Bot <bottoken>",
"Content-Type": "application/json"
}
payload = {
'access_token' : str(access_token)
}
response = requests.put(url=url, json=payload, headers=headers)
print(response.text)
The userID is enough I am not sure why you want the payload or access token; Try to drop the payload part.
If you are trying to add a user, the user has to agree to grant you some permissions check here https://discord.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes so the token I think will be the "Bearer" tokens. Check the doc I've shared.

Send body of POST request

I inspected how a request was sent to a website in firefox:
(Unfortunately I had to change the website URL to a fake one to prevent the server form being requested too much).
I tried to do this request in python:
import requests
import json
seq = 'ATGGCAGACTCTATTGAGGTC'
url = 'http://www.test.com'
body = {'QUERY': seq}
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.post(url, data=json.dumps(body), headers=headers)
print(r.text)
However when doing this the website says: Empty gene sequence passed for blast analysis. Please enter a valid gene sequence. So that means that the sequence (i.e. QUERY) is not sent correctly to the server. What am I missing here?
(P.s. hopefully missing of the website is not a problem to answer this question, if it is please let me know maybe I can ask to mention their website)
I am guessing the string / sequence that you are submitting to that particular website is the problem. I ran your sample code against a POST accepting website:
import requests
import json
seq = 'ATGGCAGACTCTATTGAGGTC'
url = 'http://httpbin.org/post'
body = {'QUERY': seq}
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.post(url, data=json.dumps(body), headers=headers)
print(r.text)
And got this result, which shows your query properly formed:
{
"args": {},
"data": "{\"QUERY\": \"ATGGCAGACTCTATTGAGGTC\"}",
"files": {},
"form": {},
"headers": {
"Accept": "text/plain",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "34",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.22.0"
},
"json": {
"QUERY": "ATGGCAGACTCTATTGAGGTC"
},
"origin": "2.122.222.8, 2.122.222.8",
"url": "https://httpbin.org/post"
}
Are you sure the it's supposed to be "QUERY=<>?" Cause it could be incorrect formatting of the body. Normally it's in JSON format as in "title: information." Note the ':' rather than the '='

Categories