urequest.post not working in micropython
Posted: Mon May 23, 2022 2:40 pm
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:
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.
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.
Code: Select all
import urequests as requests
response = requests.post("http://jsonplaceholder.typicode.com/posts", data = "some dummy content")
print(response.text)
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()
<!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.