skip to Main Content

I’m having a problem building a Twitter random quotes generator API. I’m following this tutorial:

https://www.twilio.com/blog/build-deploy-twitter-bots-python-tweepy-pythonanywhere

But I get an error that he doesn’t have. This is the code:

import requests 

api_key = '*****' 
api_url = 'https://andruxnet-random-famous-quotes.p.rapidapi.com'

headers = {'afd9cbe77emshf06f5cb2f889689p1ca1c3jsne6e79ad808cc' : 
api_key, 'http://andruxnet-random-famous-quotes.p.rapidapi.com' : 
api_url}

# The get method is called when we 
# want to GET json data from an API endpoint
quotes = requests.get(quotes = requests.get(api_url, 
headers=headers)

print(quotes.json())

And this is the error:

File "twitter_bot.py", line 12
print(quotes.json())

SyntaxError: invalid syntax

What am I doing wrong?? (I put *** on the key on purpose, I know the proper key is supposed to go there)

Thank you!

2

Answers


  1. You have a copy-and-paste error; somehow you’ve put quotes = requests.get( twice.

    It should just be:

    # The get method is called when we 
    # want to GET json data from an API endpoint
    quotes = requests.get(api_url, headers=headers)
    
    print(quotes.json())
    
    Login or Signup to reply.
  2. Tutorial is not so old but it seems it is already out of date.

    Using example from RapidAPI documentation (for Random Famous Quotes API) I created Python’s code which gives some information from server (but still not quotes)

    import requests 
    
    url = "https://andruxnet-random-famous-quotes.p.rapidapi.com/?count=10&cat=famous"
    
    headers={
        "X-RapidAPI-Host": "andruxnet-random-famous-quotes.p.rapidapi.com",
        "X-RapidAPI-Key": "afd9cbe77emshf06f5cb2f889689p1ca1c3jsne6e79ad808cc",
    }
    
    quotes = requests.get(url, headers=headers)
    
    print(quotes.text)
    #print(quotes.json())
    

    Result:

    {"message":"You are not subscribed to this API."}
    

    The same for POST

    import requests 
    
    url = "https://andruxnet-random-famous-quotes.p.rapidapi.com/?count=10&cat=famous"
    
    headers={
        "X-RapidAPI-Host": "andruxnet-random-famous-quotes.p.rapidapi.com",
        "X-RapidAPI-Key": "afd9cbe77emshf06f5cb2f889689p1ca1c3jsne6e79ad808cc",
        "Content-Type": "application/x-www-form-urlencoded"
    }
    
    quotes = requests.post(url, headers=headers)
    
    print(quotes.text)
    #print(quotes.json())
    

    Result:

    {"message":"You are not subscribed to this API."}
    

    It still need some work to get quotes.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search