Search code examples
pythonpython-3.xinstagraminstagram-api

Get Instagram access token using python


I get an error when using Flask and python-instagram libraries to get the access token. Any ideas would be greatly appreciated

Error I get when trying to get authorization url:

Traceback (most recent call last):
  File "C:\Python37\lib\site-packages\flask\app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Python37\lib\site-packages\flask\app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Python37\lib\site-packages\flask\app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python37\lib\site-packages\flask\_compat.py", line 35, in reraise
    raise value
  File "C:\Python37\lib\site-packages\flask\app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Python37\lib\site-packages\flask\app.py", line 1816, in full_dispatch_request
    return self.finalize_request(rv)
  File "C:\Python37\lib\site-packages\flask\app.py", line 1831, in finalize_request
    response = self.make_response(rv)
  File "C:\Python37\lib\site-packages\flask\app.py", line 1957, in make_response
    'The view function did not return a valid response. The'
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.

Here is the code that is failing:

  try:
    url = api.get_authorize_url(scope=None)
    redirect(url)
  except InstagramAPIError as e:
    print(e)

Background: I am new to python and web development and thought I would learn by creating a simple app to pull IG pics based on different search criteria such as tags. Any assistance will be useful.


Solution

  • TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.

    The error really tells all that you need to know. You need to return something in make_response() (assuming the url variable is correct and you have a route for it).

      try:
        url = api.get_authorize_url(scope=None)
        return redirect(url_for(url))
      except InstagramAPIError as e:
        print(e)
    

    You can read up more about this here.