1

My task is NOT to generate any application or website but just to write a PYTHON script which gives usernames as an input and retrieve User ID as a result.

As instagram asks for Application name, website, URI and all other stuff to be able to receive a client ID and other things to be able to use their API and I don't have an application as such, I don't qualify for it. So is there any way we can do it without the Instagram API?

If there is no other way, how to work for it?

Also I am new to python programming and connecting with API and all. It would be really helpful if someone would help me with a code to do so.

Thanks!

rohit nair
  • 240
  • 4
  • 16
  • 2
    It is best to stick with an official API if there is one. You should be able to register for the API without a website. – Selcuk Apr 20 '16 at 05:05
  • Thanks for the response @Selcuk . But I checked and its asking for all this. As i am new to the Python using an API concept, I dont know how to write the code for the same. Please help how to do so. – rohit nair Apr 20 '16 at 05:07
  • You can screen scrape but you will quickly get banned. I suggest you to register using placeholders then Google for a Python Instagram API library. – Selcuk Apr 20 '16 at 05:09
  • 1
    Ok let me try doing placeholders and Yes I got the python-instagram api thing from https://github.com/facebookarchive/python-instagram. I installed it. I dont know how to use this stuff. how would the code be to use it to simple get the user id – rohit nair Apr 20 '16 at 05:19
  • 1
    @Selcuk Hey! As you said, i registered to instagram using a placeholder. Can you please help me with a small code of how to use it? – rohit nair Apr 20 '16 at 06:43
  • Sorry, no experience with Instagram API, but you should be able to find a tutorial that explains this. – Selcuk Apr 20 '16 at 08:22
  • 1
    @Selcuk No problem mate! I got it. – rohit nair Apr 20 '16 at 09:28

4 Answers4

1

Well the answer to the question is we can use the Instagram API itself. As @Selcuk said, we can register using placeholders in the Instagram site. So then we will get the client_id and client_secret from it and also if we are registered instagram user, we will get the access token too.

The access token if hard to find, can be achieved by going to https://apigee.com/console/instagram and then in the authentication option on the top, select OAuth which will take you to the login page of instagram. After sending the query it will show you what request you made and can find the access token too.

In the python console just type:-

import urllib.request
import urllib.parse
 from instagram.client import InstagramAPI
access_token="YOUR ACCESS TOKEN&q"
api=InstagramAPI(client_id= 'YOUR CLIENT ID', client_secret='YOUR CLIENT SECRET')
scope='public_content'
url='https://api.instagram.com/v1/users/search?q=USERNAME TO SEARCH&access_token=YOUR ACCESS TOKEN'
a=urllib.request.urlopen(url)
print(a.read())

This will give you the details about the user.

rohit nair
  • 240
  • 4
  • 16
  • this will only give you the username of the access toke, this doesn't help with other users. – Bob May 04 '18 at 18:58
1

I put this code together below (python 3.5.2) only using urllib and it works like a dream!

from six.moves.urllib.request import urlopen

def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        return False
id_loop = True
while id_loop == True:
    username = input('Please enter the Instagram username that you would like to find the User ID for: ') 

    link = 'http://www.instagram.com/' + username
    response = urlopen(link)
    content = str(response.read());

    start_index = (content.index('"owner": {"id": "')) + len('"owner": {"id": "')

    test_string = ''

    for collect in range(14):
        test_string = test_string + content[start_index]
        start_index = start_index + 1

    edit_string = ''

    for char in test_string:
        if is_number(char) == True:
            edit_string = edit_string + char
        else:
            edit_string = edit_string + ''

    print(username + "'s Instagram ID = " + edit_string)

As you can see, the only module that you need is urllib. This code essentially finds the Instagram page of the username that you input and sets the variable content equal to a single string consisting of the entire html code of the webpage. This string is then searched by the code to find the users Instagram ID. Hope this helps!

0

Guess things in code change but life's challenges stay... here we go.... some neat python code to make things easier!

python function username to id

from six.moves.urllib.request import urlopen


def get_insta_id(username):
    link = 'http://www.instagram.com/' + username
    response = urlopen(link)
    content = str(response.read())
    start_pos = content.find('"owner":{"id":"') + len('"owner":{"id":"')
    end_pos = content[start_pos:].find('"')
    insta_id = content[start_pos:start_pos+end_pos]
    return insta_id


print(get_insta_id("username"))
0

Well, there's a python library for that.

Installation

pip install ensta

Example

from ensta import Guest

guest = Guest()  # Guest mode doesn't require login

uid = guest.get_userid("username")
print(uid)

Source Code (GitHub)

Soni
  • 3
  • 4