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.
Related
I am not trying to get an access token to any other website using postman, I am trying to get an access token for the actual Postman website!!
I have a script that I am working to implement for Azure and I need to use postman to send a POST request and download the response body. Based on my research, to be able to use the POST request to download the response body, I need to pass Postman an access token as a parameter for my download function.
This is my auth method to get the access token for postman:
def authenticate_postman(api_key, username, password):
''' Authenticate into Postman API and get a valid access token
'''
global access_token
url = "https://api.postman.com/oauth/token"
headers = {
"X-Api-Key": api_key,
"Content-Type": "application/x-www-form-urlencoded"
}
data = {
"grant_type": "password",
"username": username,
"password": password
}
response = requests.post(url, headers=headers, data=data)
if response.status_code != 200:
print(response.json())
raise Exception("Failed to authenticate. HTTP Error {}".format(response.status_code))
access_token = response.json()["access_token"]
return access_token
api_key, username, password come from os.environ
I get this error:
{'error': {'name': 'notFound', 'message': 'Requested resource not found'}}
I am not sure why it doesn't work or how I can fix this. Any help would be appreciated!
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)
I am trying to create dm
import requests
import json
url = 'https://discordapp.com/api/v6/users/#me/channels'
token = 'token'
body = {
'recipient_id': '123456789'
}
data = {'data':json.dumps(body)}
headers = {
"Authorization": token,
"Content-Type":"application/json"
}
r = requests.post(url, data=data, headers=headers)
print(r.content)
print(r.text)
This code receives response with "400: Bad request" message.
I've read this but I can't find any examples of correct usage of this request.
I finally understood my mistake, I shouldn't pass dictionary as data but should pass a str, so I changed data=data to data=json.dumps(data) and it worked
code = "AQB40N8OxnZAjvDcS8Yq6KhL0RqvKPKLCBnwtqHTnXQhMvwSyUgNUJlqyhww6qXoXYYlLZ7MmRP8Eu1XEeG9D2m3wEEdCJVyRos6brJiILl1ynKv-EL5G5dQ3vH418h-G948THH13ndUrrM-q0CDuYA06-aEpGlTk3vxK-g3bNBtS7jYSc82ToDsFgNAjL4WLPGs03Xm1j5I0zDZ7XUJASeqoCchy3-8"
import requests
import json
headers = {
'Authorization': 'Basic YTU5MWE4OTQ3ZGM4NDIzNDg2NzVlY2Y4MTk3N2M3MmI6YmRiZGJmNmY4Yjg5NDdlNGJhYzM5MzUxMTNmNmVlOGI=',
}
data = {
'grant_type': 'authorization_code',
'code': "AQB40N8OxnZAjvDcS8Yq6KhL0RqvKPKLCBnwtqHTnXQhMvwSyUgNUJlqyhww6qXoXYYlLZ7MmRP8Eu1XEeG9D2m3wEEdCJVyRos6brJiILl1ynKv-EL5G5dQ3vH418h-G948THH13ndUrrM-q0CDuYA06-aEpGlTk3vxK-g3bNBtS7jYSc82ToDsFgNAjL4WLPGs03Xm1j5I0zDZ7XUJASeqoCchy3-8",
'redirect_uri': 'http://localhost:8888'
}
response = requests.post('https://accounts.spotify.com/api/token', headers=headers, data=data)
print(response)
i dont understand why it doesnt work? what's wrong with it exactly? i followed https://developer.spotify.com/documentation/general/guides/authorization-guide/
I think it's because redirect_uri is not encoded.
try this way...
data = {
...
'redirect_uri' : 'http%3A%2F%2Flocalhost%3A8888'
}
I am trying to insert a destination order using action=insertDestinationOrder
I am using POST method with all required parameters, but keep getting
{
"errorCode": 40,
"errorMsg": "general error"
}
I have checked using postman too. But still same.
Below is the request using python requests package.
import requests
url = "https://csv.telematics.tomtom.com/extern"
payload = "orderid=TO0049&country=DE&city=Cologne&latitude=50974519&ordertype=delivery%20order&zip=50735&longitude=6977319&street=Am%20Niehler%20Hafen%20%26%20Stapelkai%2C%2050735%20Cologne%20(Niehl)&account=XXXX&username=XXXX&password=XXXX&apikey=XXXX&lang=en&action=insertDestinationOrder&ordertext=Am%20Niehler%20Hafen%20%26%20Stapelkai%2C%2050735%20Cologne%20(Niehl)&useUTF8=true&outputformat=json"
headers = {
'content-type': "application/x-www-form-urlencoded"
}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)
Need some help.
Try adding a ? to the end of the url, or to the start of the payload:
url = "https://csv.telematics.tomtom.com/extern?"
or
payload = "?orderid=TO0049&country=DE&city=Cologne&latitude=50974519&ordertype=delivery%20order&zip=50735&longitude=6977319&street=Am%20Niehler%20Hafen%20%26%20Stapelkai%2C%2050735%20Cologne%20(Niehl)&account=XXXX&username=XXXX&password=XXXX&apikey=XXXX&lang=en&action=insertDestinationOrder&ordertext=Am%20Niehler%20Hafen%20%26%20Stapelkai%2C%2050735%20Cologne%20(Niehl)&useUTF8=true&outputformat=json"