Home > Blockchain >  urequests only working on specific websites
urequests only working on specific websites

Time:05-28

I am using a nodemcu esp8266 and the plan is to make a spotify api request to automate playing some songs, and I have a fully functional python script that does exactly that, but when I tried to convert it to upython it failed. I have now spent hours on this and have figured out that the problem is that the urequest for some reason only works on specific websites, for example if I try:

import urequests as requests

x = requests.get('https://www.spotify.com')
print(x.status_code)

i get a 302

but when I try:

import urequests as requests

x = requests.get('https://www.google.com')
print(x.status_code)

I get a 200. That problem percists with a few websites and with no valid response my urequests.put command does not return the things I need... any ideas how to fix it? thank you in advance.

this is the code I am trying to run:

import urequests as requests
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"
    
    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()

CodePudding user response:

There are several things going on here that are going to cause your problems.

First, you're trying to send JSON data to the /api/token endpoint:

    data = (json.dumps(payload)).encode()
    
    response = requests.post(query,
                              data=data,
                              headers=headers)

...but according to the documentation, this endpoint excepts application/x-www-form-urlencoded data.

Second, while the documentation suggests you need to send a basicAuthorization header, in my experiments this evening I wasn't able to get that to work...but I was able to successfully refresh a token if I included the client id and secret in the request body.

You can see my forum post with this question here.

With that in mind, the following code seems to work on my esp8266 running micropython 1.18:

import urequests as requests

refresh_token = "..."
client_id = "..."
client_secret = "..."


class Refresh:
    def __init__(self, refresh_token, client_id, client_secret):
        self.refresh_token = refresh_token
        self.client_id = client_id
        self.client_secret = client_secret

    def refresh(self):
        url = "https://accounts.spotify.com/api/token"
        data = "&".join(
            [
                "grant_type=refresh_token",
                f"refresh_token={self.refresh_token}",
                f"client_id={self.client_id}",
                f"client_secret={self.client_secret}",
            ]
        )

        headers = {
            "content-type": "application/x-www-form-urlencoded",
        }

        response = requests.post(url, data=data, headers=headers)

        print(data)
        print(response.status_code)
        print(response.reason)
        print(response.text)


a = Refresh(refresh_token, client_id, client_secret)
a.refresh()
  • Related