0

Using v5 of the pinterest api and stuck on the authentication flow: https://developers.pinterest.com/docs/getting-started/authentication/

Completed the first step and got the access code. However, I get the below error when I try to use this code to get the access token.

{"code":1,"message":"Missing request body"}

Here is my code:

client_id= 'my_client_id'
client_secret = 'my_client_secret'

data_string = f'{client_id}:{client_secret}'

token = base64.b64encode(data_string.encode())

headers = {
    'Authorization': 'Basic ' + token.decode('utf-8'),
    'Content-Type': 'application/x-www-form-urlencoded',
}

url = "https://api.pinterest.com/v5/oauth/token"
code = "my_code_that_i_got_in_the_first_step"

params = {
    'grant_type':'authorization_code',
    'code': code,
    'redirect_url':'https://my_redirect_uri'
}
r = requests.post(url, headers=headers, params=params)
print(r.json())
  • 1
    You want to send that as data, not params. https://stackoverflow.com/questions/15900338/python-request-post-with-param-data – Amos Baker Aug 08 '22 at 18:50
  • Sending it as data is giving me a new error: `{'code': 1, 'message': 'Invalid parameters.'}`. I changed the `params` to `data` in the POST request. – notDarkMatter Aug 09 '22 at 04:30
  • 1
    Nevermind. There was a typo. Used redirect_url instead of uri ... smh... thanks for the tip on sending it as data @AmosBaker – notDarkMatter Aug 09 '22 at 04:34

1 Answers1

1

Below is the correct way to get the access token:

client_id= 'my_client_id'
client_secret = 'my_client_secret'

data_string = f'{client_id}:{client_secret}'

token = base64.b64encode(data_string.encode())

headers = {
    'Authorization': 'Basic ' + token.decode('utf-8'),
    'Content-Type': 'application/x-www-form-urlencoded',
}

url = "https://api.pinterest.com/v5/oauth/token"
code = "my_code_that_i_got_in_the_first_step"

data= {
    'grant_type':'authorization_code',
    'code': code,
    'redirect_uri':'https://my_redirect_uri'
}
r = requests.post(url, headers=headers, data=data)
print(r.json())

In my question, I had mistyped redirect_uri as redirect_url. Also, when sending a POST, you should use data instead of params. See the comment by Amos Baker.