dropbox.rest.ErrorResponse: [400] u'invalid_client' dropbox api fom python - python

Following the tutorial from the python api I get this error
Traceback (most recent call last):
File "/home/user/PycharmProjects/api/apid.py", line 17, in <module>
access_token, user_id = flow.finish(code)
File "/usr/local/lib/python2.7/dist-packages/dropbox/client.py", line 1398, in finish
return self._finish(code, None)
File "/usr/local/lib/python2.7/dist-packages/dropbox/client.py", line 1265, in _finish
response = self.rest_client.POST(url, params=params)
File "/usr/local/lib/python2.7/dist-packages/dropbox/rest.py", line 316, in POST
return cls.IMPL.POST(*n, **kw)
File "/usr/local/lib/python2.7/dist-packages/dropbox/rest.py", line 254, in POST
post_params=params, headers=headers, raw_response=raw_response)
File "/usr/local/lib/python2.7/dist-packages/dropbox/rest.py", line 227, in request
raise ErrorResponse(r, r.read())
dropbox.rest.ErrorResponse: [400] u'invalid_client'
And the code its the same of the tutorial
#!/usr/bin/env python
import dropbox
app_key = 'key'
app_secret = 'secret'
flow = dropbox.client.DropboxOAuth2FlowNoRedirect(app_key, app_secret)
authorize_url = flow.start()
print '1. Go to: ' + authorize_url
print '2. Click "Allow" (you might have to log in first)'
print '3. Copy the authorization code.'
code = raw_input("Enter the authorization code here: ").strip()
access_token, user_id = flow.finish(code)
client = dropbox.client.DropboxClient(access_token)
print 'linked account: ', client.account_info()
I haven't changed anything from the original code

Related

Error using Spotipy: spotipy.oauth2.SpotifyOauthError: error: invalid_client, error_description: Invalid client

I've been wanting to start a small spotify-based project and I'm currently trying to utilize python to create a playlist using the spotipy library as such:
from spotipy.oauth2 import SpotifyClientCredentials
from spotipy.oauth2 import SpotifyOAuth
import spotipy.util as util
scope = 'playlist-modify-public'
username = 'aaronang_'
token = SpotifyOAuth(scope=scope,username=username)
spotifyObject = spotipy.Spotify(auth_manager = token)
playlist_name = input("Enter a playlistname:")
playlist_description = input("Enter a playlist description:")
spotifyObject.user_playlist_create(user=username,name=playlist_name,public=True,description=playlist_description)
I set my client id,client secret and redirect uri in terminal's virtual environment(venv) yet using:
set CLIENT_ID=c3032b421ce94......9a05abcb623da
set CLIENT_SECRET=32a9c32611......5b69cf643f7c33e
set CLIENT_REDIRECT_URI=http://127.0.0.1:8080/
I end up getting this error:
Traceback (most recent call last):
File "C:\Users\Aaron\spotifyPlaylist.py", line 17, in <module>
spotifyObject.user_playlist_create(user=username,name=playlist_name,public=True,description=playlist_description)
File "C:\Users\Aaron\venv\lib\site-packages\spotipy\client.py", line 784, in user_playlist_create
return self._post("users/%s/playlists" % (user,), payload=data)
File "C:\Users\Aaron\venv\lib\site-packages\spotipy\client.py", line 302, in _post
return self._internal_call("POST", url, payload, kwargs)
File "C:\Users\Aaron\venv\lib\site-packages\spotipy\client.py", line 221, in _internal_call
headers = self._auth_headers()
File "C:\Users\Aaron\venv\lib\site-packages\spotipy\client.py", line 212, in _auth_headers
token = self.auth_manager.get_access_token(as_dict=False)
File "C:\Users\Aaron\venv\lib\site-packages\spotipy\oauth2.py", line 525, in get_access_token
token_info = self.validate_token(self.cache_handler.get_cached_token())
File "C:\Users\Aaron\venv\lib\site-packages\spotipy\oauth2.py", line 380, in validate_token
token_info = self.refresh_access_token(
File "C:\Users\Aaron\venv\lib\site-packages\spotipy\oauth2.py", line 596, in refresh_access_token
self._handle_oauth_error(http_error)
File "C:\Users\Aaron\venv\lib\site-packages\spotipy\oauth2.py", line 146, in _handle_oauth_error
raise SpotifyOauthError(
spotipy.oauth2.SpotifyOauthError: error: invalid_client, error_description: Invalid client
I've seen this code work for others, yet my token won't validate. I've got a token from https://developer.spotify.com/console/put-playlist-tracks/ which worked.
Thanks.

401 HTTP Response, when I load client secret and client-ID from praw.ini

Recently, I started a PRAW project aiming to scrape from the r/todayilearned subreddit. Browsing through the docs, if found that the best way to load up the client-id, client secret, username, and password was to store it in the praw.ini file.
This is the format I used where the ".........." were filled by the respective inputs.
[TIL]
client_id="´............"
client_secret="............"
password="............"
username=".........."
user_agent="TIL by u/........"
I executed this code and I get
import praw
reddit = praw.Reddit("TIL")
subreddit = reddit.subreddit('learnpython')
Traceback (most recent call last):
File "C:\Users\HP\Desktop\python\TIL\src\main.py", line 7, in <module>
for submission in subreddit.get_hot():
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\praw\models\reddit\base.py", line 34, in __getattr__
self._fetch()
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\praw\models\reddit\subreddit.py", line 584, in _fetch
data = self._fetch_data()
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\praw\models\reddit\subreddit.py", line 581, in _fetch_data
return self._reddit.request("GET", path, params)
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\praw\reddit.py", line 849, in request
return self._core.request(
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\prawcore\sessions.py", line 328, in request
return self._request_with_retries(
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\prawcore\sessions.py", line 226, in _request_with_retries
response, saved_exception = self._make_request(
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\prawcore\sessions.py", line 183, in _make_request
response = self._rate_limiter.call(
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\prawcore\rate_limit.py", line 33, in call
kwargs["headers"] = set_header_callback()
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\prawcore\sessions.py", line 281, in _set_header_callback
self._authorizer.refresh()
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\prawcore\auth.py", line 379, in refresh
self._request_token(
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\prawcore\auth.py", line 155, in _request_token
response = self._authenticator._post(url, **data)
File "C:\Users\HP\AppData\Local\Programs\Python\Python39\lib\site-packages\prawcore\auth.py", line 38, in _post
raise ResponseException(response)
prawcore.exceptions.ResponseException: received 401 HTTP response
But, when I do this, It works.
import praw
reddit = praw.Reddit(
client_id="´............",
client_secret="............",
password="............",
username="..........",
user_agent="TIL by u/........"
)
subreddit = reddit.subreddit('learnpython')
How can I fix this?
Thanks in Advance.
I tested it and it has to be without " "
[TIL]
client_id=2Ca......Mh4
client_secret=Bq7............X0z
password=SeCrEtPaSsWoRd
username=james_bond
user_agent=TIL by u/james_bond
but it can use spaces to make it more readable
[TIL]
client_id = 2Ca......Mh4
client_secret = Bq7............X0z
password = SeCrEtPaSsWoRd
username = james_bond
user_agent = TIL by u/james_bond
BTW:
It may also use : instead of =
[TIL]
client_id:2Ca......Mh4
client_secret:Bq7............X0z
password:SeCrEtPaSsWoRd
username:james_bond
user_agent:TIL by u/james_bond
[TIL]
client_id : 2Ca......Mh4
client_secret : Bq7............X0z
password : SeCrEtPaSsWoRd
username : james_bond
user_agent : TIL by u/james_bond
EDIT:
I checked documentation praw.ini Files and it shows examples also without " "

how to query for GET /v2/organizations/:guid/managers using cf-python-client module

I am using cf-python-client module downloaded from this link and I am trying to get managers of an organization
I am able to get the organisation information by this
client.v2.organizations.get(org_guid) but now I want to get the managers associated with this org and the API required is GET /v2/organizations/:guid/managers but i dont know how to use it through cf-python-client module.
This is how i am connecting to Cloudfoundry
from cloudfoundry_client.client import CloudFoundryClient
client = CloudFoundryClient(target_endpoint, proxy=proxy, verify=False)
client.init_with_user_credentials( 'admin', 'password' )
i tried this client.v2.organizations._get("/"+ORG_GUID+"/managers") and got this
error:
Traceback (most recent call last):
File "orgs.py", line 31, in <module>
print client.v2.organizations._get("/"+ORG_GUID+"/managers")
File "/usr/local/lib/python2.7/dist-
packages/cloudfoundry_client/v2/entities.py", line 54, in _get
response = self.client.get(url)
File "/usr/local/lib/python2.7/dist-
packages/cloudfoundry_client/client.py", line 167, in get
return CloudFoundryClient._check_response(response)
File "/usr/local/lib/python2.7/dist-
packages/cloudfoundry_client/client.py", line 194, in _check_response
raise InvalidStatusCode(response.status_code, body)
cloudfoundry_client.errors.InvalidStatusCode: 404 : {"code": 10000,
"error_code": "CF-NotFound", "description": "Unknown request"}
Try this :)
from cloudfoundry_client.client import CloudFoundryClient
import os
target_endpoint = '<API_URL>'
proxy = dict(http=os.environ.get('HTTP_PROXY', ''), https=os.environ.get('HTTPS_PROXY', ''))
client = CloudFoundryClient(target_endpoint, proxy=proxy, verify=False)
client.init_with_user_credentials('<EMAIL>', '<PASSWORD>')
org_get = client.v2.organizations.get('<GUID>')
print(org_get)
for m in org_get.managers():
print(m)

"invalid_grant error processing request" while making a reddit bot

Im making a reddit bot that replies something to a specific comment.
But Im getting this error : invalid_grant error processing request
and I can't find the solution.
here is my code, Im using Python.
import praw
import time
import config
def login():
r = praw.Reddit(user_agent = "test bot",
username = config.username,
password = config.password,
client_id = config.client_id,
client_secret = config.client_secret)
print("logged in")
return r
cache = []
def run_bot(r):
subreddit = r.subreddit("Test")
comments = subreddit.get_comments(limit=25)
for comment in comments:
comment_text = comment.body.lower()
if "xD" in comment_text and comment.id not in cache:
comment.reply("xD")
cache.append(comment.id)
while True:
r = login()
run_bot(r)
time.sleep(5)
traceback:
logged in
Traceback (most recent call last):
File "xdbot.py", line 28, in <module>
run_bot(r)
File "xdbot.py", line 19, in run_bot
comments = subreddit.get_comments(limit=25)
File "D:\Programming\Phyton\lib\site-packages\praw\models\reddit\base.py", line 31, in __getattr__
self._fetch()
File "D:\Programming\Phyton\lib\site-packages\praw\models\reddit\base.py", line 66, in _fetch
params=self._info_params)
File "D:\Programming\Phyton\lib\site-packages\praw\reddit.py", line 367, in get
data = self.request('GET', path, params=params)
File "D:\Programming\Phyton\lib\site-packages\praw\reddit.py", line 451, in request
params=params)
File "D:\Programming\Phyton\lib\site-packages\prawcore\sessions.py", line 174, in request
params=params, url=url)
File "D:\Programming\Phyton\lib\site-packages\prawcore\sessions.py", line 108, in _request_with_retries
data, files, json, method, params, retries, url)
File "D:\Programming\Phyton\lib\site-packages\prawcore\sessions.py", line 93, in _make_request
params=params)
File "D:\Programming\Phyton\lib\site-packages\prawcore\rate_limit.py", line 32, in call
kwargs['headers'] = set_header_callback()
File "D:\Programming\Phyton\lib\site-packages\prawcore\sessions.py", line 134, in _set_header_callback
self._authorizer.refresh()
File "D:\Programming\Phyton\lib\site-packages\prawcore\auth.py", line 328, in refresh
password=self._password)
File "D:\Programming\Phyton\lib\site-packages\prawcore\auth.py", line 142, in _request_token
payload.get('error_description'))
prawcore.exceptions.OAuthException: invalid_grant error processing request
Double check your credentials as this note says.
Remember that the username is your reddit's account name, not the bot's name.
Another possibility is your bot has been timed out for too many login attempts.
An easy way to check this by trying to log into Reddit manually and checking if you're blocked.
You might be using two-factor authentication with this account.
You have to disable it, or pass the 2FA code with the password in one stiring separated by a colon, like this:
r = praw.Reddit(user_agent = "test bot",
username = config.username,
password = f"{config.password}:{2FA-Code}",
client_id = config.client_id,
client_secret = config.client_secret)
See this in the docs:
https://praw.readthedocs.io/en/latest/getting_started/authentication.html#two-factor-authentication
Also, your code won't work because you use the "lower" command and there's an uppercase letter in the string you compare.

Working with Twitter API v1.1

I launched this code via terminal via command python py/twi.py and it shows no reaction:
import oauth, tweepy
from time import sleep
message = "hello"
def init():
global api
#confident information
consumer_key = "***"
consumer_secret = "***"
callback_url = "https://twitter.com/Problem196"
access_key="***"
access_secret="***"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret, callback_url)
auth.set_access_token(access_key, access_secret)
api=tweepy.API(auth)
init()
api.update_status(message)
But it supposed to post tweet "hello" on page https://twitter.com/problem196 .
I have updated tweepy, it's fine. Why it's not posting? I have no idea. Please help.
UPD: After I put code print api.last_response.msg terminal showed me some errors:
artem#artem-VirtualBox:~$ python py/twi.py
Traceback (most recent call last):
File "py/twi.py", line 20, in <module>
api.update_status(message)
File "/usr/local/lib/python2.7/dist-packages/tweepy-2.1-py2.7.egg/tweepy/binder.py", line 197, in _call
return method.execute()
File "/usr/local/lib/python2.7/dist-packages/tweepy-2.1-py2.7.egg/tweepy/binder.py", line 154, in execute
raise TweepError('Failed to send request: %s' % e)
tweepy.error.TweepError: Failed to send request: [Errno -2] Name or service not known
artem#artem-VirtualBox:~$ python py/twi.py
Traceback (most recent call last):
File "py/twi.py", line 20, in <module>
api.update_status(message)
File "/usr/local/lib/python2.7/dist-packages/tweepy-2.1-py2.7.egg/tweepy/binder.py", line 197, in _call
return method.execute()
File "/usr/local/lib/python2.7/dist-packages/tweepy-2.1-py2.7.egg/tweepy/binder.py", line 173, in execute
raise TweepError(error_msg, resp)
tweepy.error.TweepError: [{'message': 'Status is a duplicate', 'code': 187}]

Categories