Search code examples
c#uwpinstagraminstagram-apiinstasharp

Getting an error with Instagram when trying to authenticate


In a Universal Windows App (UWP), I'm trying to log the user into his/her Instagram account, but have no luck. I keep getting the following error after the user enters their credentials and pressed the login button:

enter image description here

Here is the code being used:

var scopes = new List<OAuth.Scope>();
scopes.Add(InstaSharp.OAuth.Scope.Basic);

var link = InstaSharp.OAuth
                .AuthLink
                (
                    _config.OAuthUri + "authorize",
                    _config.ClientId,
                    _config.RedirectUri,
                    scopes,
                    InstaSharp.OAuth.ResponseType.Token
                );

var webAuthResult = await WebAuthenticationBroker
                                .AuthenticateAsync
                                (
                                    WebAuthenticationOptions.None,
                                    new Uri(link)
                                );

switch (webAuthResult.ResponseStatus)
{
    case WebAuthenticationStatus.Success:
        return true;

    case WebAuthenticationStatus.UserCancel:
        return false;

    case WebAuthenticationStatus.ErrorHttp:
        return false;

    default:
        return false;
}

Am I doing something wrong??


UPDATE 1:


Here is a UWP quick sample that demonstrates the issue:

UWP SAMPLE


UPDATE 2:


I found the problem that caused the "cannot connect to service" error. Here was the code that caused the problem:

        _config = new InstagramConfig(clientIdTextBox.Text,
                                clientSecretTextBox.Text,
                                WebAuthenticationBroker.GetCurrentApplicationCallbackUri().ToString(),
                                WebAuthenticationBroker.GetCurrentApplicationCallbackUri().ToString());

My 3rd parameter, the Redirect Url, is not a web address, but an application link. If I were to change that to a simple Url like www.instagram.com, then it works, HOWEVER the browser stays open on that redirect url.

How do I make the browser redirect back to my UWP app using the redirect Url ???


Solution

  • I found my problem!

    In the end, my redirect Url was not matching exactly to the Url I provided in my Instragram developer account, that was causing the issue.

    UPDATE:

    Although I fixed my problem, and I receive my access code correctly, it seems there is a bigger problem with Instragram because I always receive a 400 Bad Request. And from what I've seen around online from other developers, this is a very common problem. I'm in a sandbox environment, I wonder if that has anything to do with it.