abjerner / Skybrud.Social.Instagram

.NET wrapper and API implementation for the Instagram Basic Display API and Instagram Graph API.
https://packages.limbo.works/skybrud.social.instagram/
MIT License
13 stars 5 forks source link

GetAccessTokenFromAuthCode(authUrl) doesn't give access token #3

Closed biplovkc closed 7 years ago

biplovkc commented 7 years ago

This is what I am doing right now

``

    static string clientId = ConfigurationManager.AppSettings["client_id"];
    static string clientSecret = ConfigurationManager.AppSettings["client_secret"];
    static string redirectUri = ConfigurationManager.AppSettings["redirectUri"];

        InstagramConfig config = new InstagramConfig(clientId, clientSecret, redirectUri, "");
        InstagramOAuthClient client = new InstagramOAuthClient
        {
            ClientId = clientId,
            ClientSecret = clientSecret,
            RedirectUri = redirectUri
        };

        public ActionResult Login()
        {
            string authorizationUrl = client.GetAuthorizationUrl("insta");        

            return Redirect(authorizationUrl); //redirects to OAuth with code as parameter
        }
        public ActionResult OAuth(string code)
        {

            InstagramAccessTokenResponse response = client.GetAccessTokenFromAuthCode(code);
            Session.Add("AuthInfo", response);
            return RedirectToAction("Index");
        }

I get error in line InstagramAccessTokenResponse response = client.GetAccessTokenFromAuthCode(code);

An exception of type 'System.NullReferenceException' occurred in Skybrud.Social.dll but was not handled in user code

Additional information: Object reference not set to an instance of an object.

I am getting the code values but the access token doesn't work after getting the code

abjerner commented 7 years ago

@cybercop I'll look into that ;)

What version of Skybrud.Social are you using?

biplovkc commented 7 years ago

0.9.6

abjerner commented 7 years ago

@cybercop I haven't been able to reproduce the issue in my end.

Are you sure that neither ClientId, ClientSecret or RedirectUri are empty or null when calling GetAccessTokenFromAuthCode? If at least one of them is missing, then I'm able to reproduce the same exception.

biplovkc commented 7 years ago

error1

error2

nope they are not null

abjerner commented 7 years ago

The Instagram API is most likely responding with an error, which isn't handled properly in my code. So in your OAuth method, could you try replacing the existing with the following code:


        // Initialize collection with POST data
        NameValueCollection parameters = new NameValueCollection {
            {"client_id", client.ClientId},
            {"client_secret", client.ClientSecret},
            {"grant_type", "authorization_code"},
            {"redirect_uri", client.RedirectUri},
            {"code", code }
        };

        // Make the call to the API
        string body = SocialUtils.DoHttpPostRequestAndGetBodyAsString("https://api.instagram.com/oauth/access_token", null, parameters);

and then post the contents of body? The API should respond with a JSON body - even for error responses.

Alternatively, you could try installing Skybrud.Social.Instagram (the package in this repository, actually) instead. Right now you have Skybrud.Social, which is currently the stable version, and which has support for a number of services (including Instagram) by default.

Skybrud.Social will soon be replaced by individual packages instead, where Skybrud.Social.Instagram (still beta'ish at the moment) will be the package for Instagram. Skybrud.Social.Instagram already has better error checking than Skybrud.Social ;)

biplovkc commented 7 years ago

I installed Skybrud.Social.Instagram as you said. But the error now says invalid client secret. Which clearly is not

error1

error2

error3

I also reset my secret and tried the new secret key but still same error.

If my client secret was wrong would I still get code for access token?

abjerner commented 7 years ago

In the screenshot above, doesn't your client secret have a space at the end?

biplovkc commented 7 years ago

Ah sorry for the stupidity. it works now thank you! Is there any documentation regarding, how to get some pictures by username? (lets say 20 images)

abjerner commented 7 years ago

No worries ;)

Regarding looking up pictures by a given username: you can't. But you can do a search first for the username first to find the ID of the user, and then request images for that user ID.

here's some documentation:

  1. Search users
  2. Getting a list of recent media