Page 1 of 1

urequest.post not working in micropython

Posted: Mon May 23, 2022 2:40 pm
by jonny
hello, I am trying to use the spotify api with my nodemcu esp8266 and it works fine in python but not in micropython.. when I run a post request like this:

Code: Select all

import urequests as requests
response = requests.post("http://jsonplaceholder.typicode.com/posts", data = "some dummy content")
print(response.text)
it work fine and give me a response so the internet connection cannot be the problem, but when I try my actuall code it cant seem to get the data from the website.

Code: Select all

import urequests
import ujson as json 

refresh_token = "xxxxx"
base_64 = "xxxxx"


class Refresh:

    def __init__(self):
        self.refresh_token = refresh_token
        self.base_64 = base_64

    def refresh(self):

        query = "https://accounts.spotify.com/api/token"
        
        data={"grant_type": "refresh_token", "refresh_token": refresh_token}
        
        response = urequests.post(url='https://accounts.spotify.com/api/token', data=json.dumps(data),
                                  headers={"Authorization": "Basic " + base_64})

        print(response.text)

    
a = Refresh()
a.refresh()
in the original code I use json to save the data but that just returns an error at the moment and the .text seems to work, but intead of giving me an access token like it should it just returns this:

<!DOCTYPE html>
<html ng-app="accounts" ng-csp>
<head>
<meta charset="utf-8">
<title>Error - Spotify</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<base href="/">
<link rel="icon" href="https://accounts.scdn.co/oauth2/images/ ... 1ac1fa.ico">
<link href="" media="screen" rel="stylesheet">
</head>
<body>
<div class="head">
<a class="spotify-logo" href="/" tabindex="-1" title="Spotify"></a>
</div>

<div class="container-fluid error">
<div class="content">
<h1 class="h1">Error</h1>
<p>
Oops! Something went wrong, please try again or check out our <a href="https://www.spotify.com/help">help area</a>.
</p>
</div>
</div>
<script async defer src="{2}" sp-error='{3}'></script>
</body>
</html>


any idea how to get around this? the only difference I had to make is the json.dumps instead of just headers=headers because it kept returning a "TypeError: object with buffer protocol required" error message. I have looked everywhere but i cant seem to find a solution...
Thank you so much in advance for your help.

Re: urequest.post not working in micropython

Posted: Wed May 25, 2022 12:02 pm
by KJM
You can use https://stackoverflow.com/questions/374 ... re-sending or https://stackoverflow.com/questions/105 ... pplication to compare python Vs upython post & then mod the latter to match the former.

Re: urequest.post not working in micropython

Posted: Wed May 25, 2022 4:32 pm
by jonny
Ok, so I have checked everything and have found out the problem, I just do not know how to fix it.
if I try:

Code: Select all

import urequests as requests

x = requests.get('https://www.google.com')
print(x.status_code)
it works fine and gives me a 200
but when i try another website like spotify:

Code: Select all

import urequests as requests

x = requests.get('https://www.spotify.com')
print(x.status_code)
I get a 302.. so apparently urequest cannot establish connections with specific websites, any ideas how to fix that?

Re: urequest.post not working in micropython

Posted: Wed May 25, 2022 9:26 pm
by KJM
Correct, 302 is a redirect & urequests can't handle redirects.

Re: urequest.post not working in micropython

Posted: Thu May 26, 2022 5:17 am
by karfas
jonny wrote:
Wed May 25, 2022 4:32 pm
I get a 302.. so apparently urequest cannot establish connections with specific websites, any ideas how to fix that?
You search the web for a http protocol description, find out what a status code 302 means and act accordingly.
Most likely you will make another request to the url you got in the redirect response.

Re: urequest.post not working in micropython

Posted: Thu May 26, 2022 1:25 pm
by jonny
yeah thank you, I have figured out why I was getting the 302 message and that that is the normal expected response, The problem is I am still getting a 400 bad request error when running the code provided in the original question and no matter how many urequest examples I look at, I cannot seem to find the mistake I am making..

Code: Select all

import urequests as requests
import ujson as json 

refresh_token = "xxxx"
base_64 = "xxxx"


class Refresh:

    def __init__(self):
        self.refresh_token = refresh_token
        self.base_64 = base_64

    def refresh(self):

        query = "https://accounts.spotify.com/api/token"
        
        payload={"grant_type": "refresh_token", "refresh_token": refresh_token}
        headers={'Authorization': 'Basic %s' % base_64}
        data = (json.dumps(payload)).encode()
        
        response = requests.post(query,
                                  data=data,
                                  headers=headers)


        print(response.status_code)
        print(response.reason)

a = Refresh()
a.refresh()

this is the urequest i am making.. any ideas what I have to fix with the parameters?

Re: urequest.post not working in micropython

Posted: Thu May 26, 2022 10:47 pm
by KJM
urequests needs an extra template for data with headers https://stackoverflow.com/questions/624 ... n-language. I know you're not doing images but the idea is still valid