9 Replies - 1785 Views - Last Post: 31 December 2017 - 01:42 AM Rate Topic: -----

#1 ikhlas06   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 327
  • Joined: 20-May 16

I am getting an error while trying to connect to instagram api

Posted 30 December 2017 - 02:09 AM

I am using aspx vb .net to connect with instagram api

I am using the following link as references: https://code.msdn.mi...n-Said-e1d8d388
ERROR: The remote server returned an error: (400) Bad Request. 


It is weird becuase i followed all steps and imported json as showed in above link. any idea? below is my code:


Dim json As String = ""

            Try
                Dim parameters As New NameValueCollection
                parameters.Add("client_id", Client_ID)
                parameters.Add("client_secret", ClientSecret)
                parameters.Add("grant_type", "authorization_code")
                parameters.Add("redirect_uri", Redirect_URI)
                parameters.Add("code", Code)

                
                Dim client As WebClient = New WebClient()

            
            Try
               'ERROR HERE
                Dim result = client.UploadValues("https://api.instagram.com/oauth/access_token", "POST", parameters)
                ...
            Catch ex As Exception
                labelTest.Text += "---" & ex.Message
            End Try

This post has been edited by ikhlas06: 30 December 2017 - 03:08 AM


Is This A Good Question/Topic? 0
  • +

Replies To: I am getting an error while trying to connect to instagram api

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5241
  • View blog
  • Posts: 14,037
  • Joined: 18-April 07

Re: I am getting an error while trying to connect to instagram api

Posted 30 December 2017 - 11:35 AM

Do realize that the article you are following is from 2014... that is over 3 years ago so things can change in what they want for security. Social media sites are notorious for trying to build things against because they often change APIs all the time as they update them. Just be mindful of this and compare what you are doing against the official Instagram API site.

Now I would first do a sanity check and make sure that all your values are coming in correctly before the access_token bit. Did you first authorize with https://api.instagra...auth/authorize/ and are you getting back an authorization code? I assume you are if that is what you are putting in the variable "Code". Just want to make sure you have that part first. Be sure to check all your values. Break point right before line 17 above and make sure all values are filled in.

Ok, so you are getting values for everything. Great. Now I would test a simple request using CURL to try it out. Hopefully you know how to use CURL. As outlined right on the Instagram API developers page (https://www.instagram.com/developer/authentication/) you can use a request like this...

curl -F 'client_id=CLIENT_ID' \
-F 'client_secret=CLIENT_SECRET' \
-F 'grant_type=authorization_code' \
-F 'redirect_uri=AUTHORIZATION_REDIRECT_URI' \
-F 'code=CODE' \
https://api.instagram.com/oauth/access_token



And of course you plug in your values including the code you got back from the authorization process. See if you get a token back. Once you do, now you have a blueprint to debug your VB.NET app and make sure you have all the pieces. My guess is you are missing some values in your request. Perhaps no authorization code? :)
Was This Post Helpful? 0
  • +
  • -

#3 ikhlas06   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 327
  • Joined: 20-May 16

Re: I am getting an error while trying to connect to instagram api

Posted 30 December 2017 - 12:59 PM

Thanks. yeah I been working on this for couple months now and trying to debug but I just have no idea whats going on. I mean I looked at insta api webbsite sill no luck. I tested my values also and they seem to be correct:

curl -F 'client_id=CLIENT_ID' \
-F 'client_secret=CLIENT_SECRET' \
-F 'grant_type=authorization_code' \
-F 'redirect_uri=AUTHORIZATION_REDIRECT_URI' \
-F 'code=CODE' \
https://api.instagra...th/access_token



 client_secret = <snipped>
 client_secret = <snipped>
 grant_type = authorization_code 
 redirect_uri = http://localhost:1861/UI/Home.aspx 
 code = 6185508825da0c28a33ac5dcc77



note, 'code' i am getting when when user logs into insta. I used the following url to get the code:
https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=code


I know authorize is correct becuase it gives me code in url

This post has been edited by Martyr2: 30 December 2017 - 01:43 PM
Reason for edit:: Cut out client secrets

Was This Post Helpful? 0
  • +
  • -

#4 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5241
  • View blog
  • Posts: 14,037
  • Joined: 18-April 07

Re: I am getting an error while trying to connect to instagram api

Posted 30 December 2017 - 01:50 PM

I am not sure if you posted real client secret or not but I suggest you change it if you did because that will have compromised your account access.

Anyways, when you used the curl command, did you get back the access token along with the user information they provide? It should look like this...

{
    "access_token": "fb2e77d.47a0479900504cb3ab4a1f626d174d2d",
    "user": {
        "id": "1574083",
        "username": "snoopdogg",
        "full_name": "Snoop Dogg",
        "profile_picture": "..."
    }
}




Let me know if you got that or not.
Was This Post Helpful? 1
  • +
  • -

#5 ikhlas06   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 327
  • Joined: 20-May 16

Re: I am getting an error while trying to connect to instagram api

Posted 30 December 2017 - 01:57 PM

View PostMartyr2, on 30 December 2017 - 01:50 PM, said:

I am not sure if you posted real client secret or not but I suggest you change it if you did because that will have compromised your account access.

Anyways, when you used the curl command, did you get back the access token along with the user information they provide? It should look like this...

{
    "access_token": "fb2e77d.47a0479900504cb3ab4a1f626d174d2d",
    "user": {
        "id": "1574083",
        "username": "snoopdogg",
        "full_name": "Snoop Dogg",
        "profile_picture": "..."
    }
}




Let me know if you got that or not.


no i am not getting back access_token.i chanted try/catch to WebException to see if i get more info but no luck. it gives same error

This post has been edited by ikhlas06: 30 December 2017 - 02:01 PM

Was This Post Helpful? 0
  • +
  • -

#6 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5241
  • View blog
  • Posts: 14,037
  • Joined: 18-April 07

Re: I am getting an error while trying to connect to instagram api

Posted 30 December 2017 - 01:58 PM

I am not talking about your VB.NET code. Did you get that back from CURL when you tried it?
Was This Post Helpful? 1
  • +
  • -

#7 ikhlas06   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 327
  • Joined: 20-May 16

Re: I am getting an error while trying to connect to instagram api

Posted 30 December 2017 - 02:10 PM

oh nope, never heard of curl commands. let me google it

oh nope, never heard of curl commands. let me google it
Was This Post Helpful? 0
  • +
  • -

#8 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5241
  • View blog
  • Posts: 14,037
  • Joined: 18-April 07

Re: I am getting an error while trying to connect to instagram api

Posted 30 December 2017 - 02:14 PM

Just so you know what I am trying to get you to do is to try the process of getting the access token outside of VB.NET so that you can make sure that all your values work and the process is known to you without having to worry about how you are doing it in VB.NET. Once you see things working, then you can go back to the VB.NET and I bet you money that you will then immediately see what you are doing wrong in VB.NET.

On the other hand, if you see that you are having problems in CURL then you can rule out everything you are doing in VB.NET and focus on why your commands are not working. It is a process of elimination exercise. :)

This post has been edited by Martyr2: 30 December 2017 - 02:14 PM

Was This Post Helpful? 1
  • +
  • -

#9 ikhlas06   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 327
  • Joined: 20-May 16

Re: I am getting an error while trying to connect to instagram api

Posted 30 December 2017 - 07:34 PM

so i you might be on to some thing here. This past couple months i been think issue is with .net code but i was wrong. know i know issue is with CURL command.

first thing i did was enter the following url in webbrower
https://api.instagram.com/oauth/authorize/?client_id=f208d9fc9ce386a&redirect_uri=http://localhost:1861/UI/Home.aspx&response_type=code

than it prompted me to insta login page where i loged in. after the redirect to my site and there is a new code in url. so far so good!!

than i enter this following url in webbrower
https://api.instagram.com/oauth/access_token/?client_id=f208d9fc9ce386a&client_secret=d836619eed12983961&grant_type=authorization_code&redirect_uri=http://localhost:1861/UI/Home.aspx&code=f2804a162c216e459


but it gave me error. know i know issue is with this url now
This page isn’t working
If the problem continues, contact the site owner.
HTTP ERROR 405


I still don't know the issue but at least i know issue is not with .net code.

This post has been edited by ikhlas06: 30 December 2017 - 07:44 PM

Was This Post Helpful? 0
  • +
  • -

#10 ikhlas06   User is offline

  • D.I.C Regular

Reputation: 1
  • View blog
  • Posts: 327
  • Joined: 20-May 16

Re: I am getting an error while trying to connect to instagram api

Posted 31 December 2017 - 01:42 AM

solved!

just changed response_type from code to token... it will give you access_token
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1