KeyError : 'groups'
When I call
results = requests.get(url).json()['response']['groups'][0]['items']
My foursquare API calls are not exhausted yet and still this error shows up every time. I even tried running it by using new client ID and client secret but the problem still persists.
I would love to have some strong solution to this issue so that I can progress further with my project.
It looks like you are parsing the json incorrectly for the latest version of the Places API. 'response', 'groups' and 'items' are not returned.
You can find all the available fields on the Places search response here --> https://developer.foursquare.com/reference/response-fields
I was able to successfully return the first item from a request with the below code. Just replace the API key with yours and it should work.
from os import system
import requests
url = "https://api.foursquare.com/v3/places/search?ll=41.8789%2C-87.6359&&radius=50000"
results = requests.get(url, headers={"Accept":"application/json", "Authorization":"yourApiKey"})
print(results.json()['results'][0])
Related
So I've been trying valorantpy module that provides an easy way to send requests to get personal player statistics.
This code:
import os
import valorant
KEY = 'key'
client = valorant.Client(KEY, locale="ru-RU", region="eu", route="europe")
account = client.get_user_by_name("SGT ItsRazyHero#VCTRU")
match = account.matchlist().history.find(queueId="competitive")
if match == None:
print("No Ranked match in recent history!")
exit(1)
else:
match = match.get()
for team in match.teams:
print(f"{team.teamId} Team's Ranks: ")
players = match.players.get_all(teamId=team.teamId)
for player in players:
print(f"\t{player.gameName} - {player.rank}")
Checks my latest competitive match and gets information about teammates and their ranks.
So I've got trouble.
requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: https://eu.api.riotgames.com/val/match/v1/matchlists/by-puuid/C3NUBHHFqy-JaV_losaGX0s_-q-NszM5VGYyHHV8GhFMVffm9GhknMFaCiY5GxPLdQ6L_cDYUQol9A?locale=ru-RU
And I don't know how to solve this problem.
If you follow this link you will get 401 Unauthorized error.
If you're using a temporary Development API key, it won't work cause you are not authorized to fetch match history. You can find a list of accessible APIs that work on a Development API key by logging in to developer.riotgames.com and going to the APIS page.
Solution:
You need to register your product and get the Production API key or the Personal API key (the choice is yours, depending upon your requirements). You can do this by logging in to the above site and registering your product.
I am trying to use this code to delete 550 tweets from my account, as I want to use it as my personal account again without the tweets from my bots:
import tweepy
import json
auth = tweepy.OAuthHandler("consumer_key", "consumer_secret")
auth.set_access_token("access_token", "access_token_secret")
api = tweepy.API(auth)
screen_name = "YOUR_TWITTER_HANDLE"
timelines = api.user_timeline(screen_name, count = 550, include_rts = False)
for status in timelines:
tweetid = status._json
api.destroy_status(tweetid)
But every time I run it, I get this error:
tweepy.error.TweepError: [{'message': 'Application cannot perform write actions. Contact Twitter Platform Operations through https://support.twitter.com/forms/platform', 'code': 261}]
When I print tweetid, it is successfully grabbing all my tweets. I can also tweet normally using api.update_status, so why is this not working? What am I doing wrong?
seems like you have made to many requests to the twitter api - from my little reasearch the error code 261 means the token was suspended. Check this links:
Why is the twitter API throwing this error?
https://blog.cotten.io/common-twitter-error-codes-6b324396042e
It sounds like your API key has been restricted (this can happen if you try to perform too many automated actions, see the automation rules https://help.twitter.com/en/rules-and-policies/twitter-automation). You will need to have the app restored via https://help.twitter.com/forms/automation
I am trying to use the Spotify API (using Spotipy), however I am having issues with the authentication step. I am following along a youtube playlist to learn it. To do that, I am just following along with the code that is shown below. However, when it opens into my web browser for authentication, I get an "Illegal redirect_uri" error.
I tried searching the web and came across this answer that says that it is probably a typo in the redirect_URI on the spotify website or that has been set in my environment variable, however, I have quadruple checked to make sure there was no typo. An image is attached that shows what my environment variable is and what the redirect_URI is set as in spotify.
Is there another reason that I could be getting this error?
Thank you for the help. Spotify Redirect_URI
import os
import sys
import json
import spotipy
import webbrowser
import spotipy.util as util
from json.decoder import JSONDecodeError
#Get the username from terminal
username = sys.argv[1]
# Erase cache and prompt for user permission
try:
token = util.prompt_for_user_token(username)
except:
os.remove(f".cache-{username}")
token = util.prompt_for_user_token(username)
#Create our spotifyObject
spotifyObject = spotipy.Spotify(auth=token)
It is probably a security issue, since google.com will not understand the parameters of the request sent by the Spotify API. It looks like you are not trying to intercept the request (since you are using google.com), so you could try to use https://localhost:8080/ as the redirect url. Since there is (probably) no server running locally, no page will open (and you will get an error), but you only need to copy the url in the address bar ;-)
I'm working with google API lately and use simple flask method to retrieve some id_token.
here is my code with explanations in comment:
#app.route('/afterlogin/id_token')
def afterlogin(id): # get the id
print(id) # print it
return render_template(r'creds_view.html', data=id) # and render the template with 'id' in it (for test purposes)
So what happens is that after the user logins, the api redirects the id_token to http://localhost:8000/afterlogin/#id_token=some_id_token.
but for some reason it is showing me 404 error.
i think it is because of the '#' in the url , i want the id_token. i know that '#' in html means for path linking or routing in 'href'.
so for that i tried.
#app.route('/afterlogin/<path:id>')
but the error still persists.
any guesses?
Everything after # is processed locally by the browser, it's not sent to the server, so you can't use it in routing. Leave out the #:
http://localhost:8000/afterlogin/some_id_token
I have created an api gateway from my existing api using boto3 import command.
apiClient = boto3.client('apigateway', awsregion)
api_response=apiClient.import_rest_api
(
failOnWarnings=True,
body=open('apifileswagger.json', 'rb').read()
)
But i cant modify integration request. I tried with following Boto3 command.
apiClient = boto3.client('apigateway', awsregion)
api_response=apiClient.put_integration
(
restApiId=apiName,
resourceId='/api/v1/hub',
httpMethod='GET',
integrationHttpMethod='GET',
type='AWS',
uri='arn:aws:lambda:us-east-1:141697213513:function:test-lambda',
)
But I got error like this
Unexpected error: An error occurred () when calling the PutIntegration operation:
I need to change lambda function region & name using Boto3 command. is it possible? .
if it is possible what is the actual issue with this command?
In the put_integration() call listed above, your restApiId and resourceId look incorrect. Here's what you should do.
After importing your rest API, check to see if it is available by calling your apiClient's get_rest_apis(). If the API was imported correctly, you should see it listed in the response along with the API's ID (which is generated by AWS). Capture this ID for future operations.
Next, you'll need to look at all of the resources associated with this API by calling your apiClient's get_resources(). Capture the resource ID for the resource you wish to modify.
Using the API ID and resource ID, check to see if an integration config exists by calling your apiClient's get_integration(). If it does exist you can modify the integration request by calling update_integration(); if it does not exist, you need to create a new integration by calling put_integration() and passing the integration request as a parameter.
Here's an example of how that might look in code:
# Import API
api_response1 = apiClient.import_rest_api(failOnWarnings=True, body=open('apifileswagger.json', 'rb').read())
print(api_response1)
# Get API ID
api_response2 = apiClient.get_rest_apis()
for endpoint in api_response2['items']:
if endpoint['name'] == "YOUR_API_NAME":
api_ID = endpoint['id']
# Get Resource ID
api_response3 = apiClient.get_resources(restApiId=api_ID)
for resource in api_response3['items']:
if resource['path'] == "YOUR_PATH":
resource_ID = resource['id']
# Check for Existing Integrations
api_response4 = apiClient.get_integration(restApiId=api_ID, resourceId=resource_ID , httpMethod='GET')
print(api_response4)
# Create Integration with Request
integration_request = { 'application/json': '{\r\n "body" : $input.json(\'$\'),\r\n}' }
api_response5 = apiClient.put_integration(restApiId=api_ID, resourceId=resource_ID , httpMethod='GET', type='AWS',
integrationHttpMethod='GET', uri="YOUR_LAMBDA_URI", requestTemplates=integration_request)
print(api_response5)
All the methods listed above are explained in the Boto3 Documentation found here.
As with most API Gateway updates to API definitions, in order to update an integration request, you have to do a PATCH and pass a body with a patch document using the expected format. See documentation here