Code Monkey home page Code Monkey logo

instagram-basic-display-python's Introduction

Instagram Basic Display Python API

A simple Python library for the Instagram Basic Display API. Based on the instagram-basic-display-php by espresso.dev

PyPI package available.

Requirements

  • Python3.6 or better
  • python-requests
  • Facebook Developer Account
  • Facebook App

Get started

To use the Instagram Basic Display API, you will need to register a Facebook app and configure Instagram Basic Display. Follow the getting started guide.

Installation

I strongly advice using PyPI to keep updates as smooth as possible.

$ pip3 install instagram-basic-display

Initialize the library

from instagram_basic_display.InstagramBasicDisplay import InstagramBasicDisplay

instagram_basic_display = InstagramBasicDisplay(app_id='YOUR_APP_ID', app_secret='YOUR_APP_SECRET', redirect_url='YOUR_APP_REDIRECT_URI')

# Optionally you can force graph version using optional parameter `graph_version`:
# instagram_basic_display = InstagramBasicDisplay(app_id='YOUR_APP_ID', app_secret='YOUR_APP_SECRET', redirect_url='YOUR_APP_REDIRECT_URI', graph_version='v16.0')


print(instagram_basic_display.get_login_url()) # Returns login URL you need to follow

Authenticate user (OAuth2)

# Get the OAuth callback code
code = request.args.get('code')

# Get the short lived access token (valid for 1 hour)
short_lived_token = instagram_basic_display.get_o_auth_token(code)

# Exchange this token for a long lived token (valid for 60 days)
long_lived_token = instagram_basic_display.get_long_lived_token(short_lived_token.get('access_token'))

print('Your token is: {}' .format(long_lived_token.access_token))

Get user profile

# Set user access token
instagram_basic_display.set_access_token(long_lived_token.access_token)

# Get the users profile
profile = instagram_basic_display.get_user_profile()

print(profile)

Available methods

Setup Instagram

Instagram(app_id: str, app_secret: str, redirect_url: str)

Get login URL

get_login_url(scopes: list=None)

Get OAuth token (Short lived valid for 1 hour)

get_o_auth_token(code: str)

Exchange the OAuth token for a Long lived token (valid for 60 days)

get_long_lived_token(access_token)

Refresh access token for another 60 days before it expires

refresh_token(access_token)

Set / Get access token

  • Set the access token, for further method calls: set_access_token($token)
  • Get the access token, if you want to store it for later usage: get_access_token()

User methods

Authenticated methods

  • get_user_profile()
  • get_user_media(user_id='me', limit: int = None, before: int = None, after: int = None)
    • if an user_id isn't defined or equals 'me', it returns the media of the logged in user

Media methods

Authenticated methods

  • get_media(_id: int)
  • get_media_children(_id: int)

Pagination

The get_user_media endpoint has a maximum range of results, so increasing the limit parameter above the limit of 99 won't help.You can use pagination to return more results for this endpoint.

Pass an object into the pagination() method and receive your next dataset:

media = instagram_basic_display.get_user_media()

more_media = instagram_basic_display.pagination(media)

instagram-basic-display-python's People

Contributors

ibrahimokdadov avatar salamek avatar stolenvw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

instagram-basic-display-python's Issues

Question

Hi, i was wondering if there is any methods that can retrieve the number of followers for an account (i got the account id already)

AttributeError: 'dict' object has no attribute 'access_token'

Hi, i am getting the error

AttributeError: 'dict' object has no attribute 'access_token'

when i try to get my long-lived token..

the original code is

long_lived_token = instagram_basic_display.get_long_lived_token(short_lived_token.get('access_token'))

please advice

No module named 'instagram_basic_display' after installing it with pip3

Hello! I have tried to use it but every time I open the file it says:

from instagram_basic_display.InstagramBasicDisplay import InstagramBasicDisplay
ModuleNotFoundError: No module named 'instagram_basic_display'

And I don't know what to do, I tried installing it again but it keeps me saying this error, any help?

Thanks

NameError: name 'request' is not defined

Hello! I tried to use the module, but every time I try to use it it shows me this error: NameError: name 'request' is not defined

I tried using: import requests, but it doesn't work.

The code i am trying to use is:

from instagram_basic_display.InstagramBasicDisplay import InstagramBasicDisplay


instagram_basic_display = InstagramBasicDisplay(app_id='app-id', app_secret='app-secret', redirect_url='https://developers.facebook.com/sa/apps/app-id')

print(instagram_basic_display.get_login_url()) # Returns login URL you need to follow

# Get the OAuth callback code
code = request.args.get('code') <- HERE IS WHERE IT SHOWS THE ERROR

# Get the short lived access token (valid for 1 hour)
short_lived_token = instagram_basic_display.get_o_auth_token(code)

# Exchange this token for a long lived token (valid for 60 days)
long_lived_token = instagram_basic_display.get_long_lived_token(short_lived_token.get('access_token'))

print('Your token is: {}' .format(long_lived_token.access_token))

# Set user access token
instagram_basic_display.set_access_token(long_lived_token.access_token)

# Get the users profile
profile = instagram_basic_display.get_user_profile()

print(profile)

Thanks

'str' object has no attribute 'args' for line code = request.args.get('code')

Please help a newbie. I am getting the error:
'str' object has no attribute 'args' for the following line of code:

code = request.args.get('code')

Here is my code:

YOUR_APP_ID = '111111111111111'
YOUR_APP_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
YOUR_REDIRECT_URL = 'https://blah.co.uk'
instagram_basic_display = InstagramBasicDisplay(app_id=YOUR_APP_ID, app_secret=YOUR_APP_SECRET, redirect_url=YOUR_REDIRECT_URL)
print(instagram_basic_display.get_login_url()) # Returns login URL you need to follow
request=instagram_basic_display.get_login_url()

code = request.args.get('code')

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.