Problem logging in using Android App


#1

I’m having an issue when logging in using the android app, I’ve used the same links used in Postman API for POST request but when I log in using the Android app it gives an internal server error, this code that I have is also used by my colleague and he can login, but my app can’t, I have no idea why, can anyone help?


#2

what is the URL you calling in your code?


#3

https://auth.mydevices.com/oauth/token


#4

This is the end point you need to call for getting access token.

curl --request POST \
  --url https://accounts.mydevices.com/auth/realms/cayenne/protocol/openid-connect/token \
  --header 'content-type: application/x-www-form-urlencoded' \
  --data 'grant_type=password&client_id=<get from web dashboard>&client_secret=<get from web dashboard>&username=<email>&password=<password>

#5

I see, I have to try this out, thank you so much!


#6

I have a similar problem using JAVA Android.
The details are here


#7

Okay so the new link works, but I’m getting a 401 Unauthorized, I think it has something to do with my code, Can anyone help me?


#8

change email to username
TokenPairs.add(new KeyValuePair<string, string>("username", HttpUtility.UrlEncode(mail)));


#9

I did change it, but it still gives me an Unauthorized error unfortunately


#10

Hi, Just an update, it finally worked! here’s the revised code if anyone stumbles upon the same problem as mine, thank you so much!!


#11

What was the issue can you point it out


#12

The issue that I was having is that my code is sending the incorrect url encoded content from the post request in the C# code, what I did is I rearranged the List first before encoding it to an x-www-form-urlencoded content, apparently i’m being redundant with the declaration of the ContentType in the previous image.


#13

Can you share your code here


#14
public async void LoginProcess(string mail, string pass, ProgressDialog Foo, AlertDialog.Builder Bar, Context context)
    {
        try
        {
            var HTTP_Client = new HttpClient();
            HTTP_Client.BaseAddress = new Uri("https://accounts.mydevices.com");
            var HTTP_Request = new HttpRequestMessage(HttpMethod.Post, "/auth/realms/cayenne/protocol/openid-connect/token");

            var RequestData = new List<KeyValuePair<string, string>>();

            RequestData.Add(new KeyValuePair<string, string>("grant_type", "password"));
            RequestData.Add(new KeyValuePair<string, string>("client_id", "[my_Client_ID]"));
            RequestData.Add(new KeyValuePair<string, string>("client_secret", "[my_Client_Secret]"));
            RequestData.Add(new KeyValuePair<string, string>("username", mail));
            RequestData.Add(new KeyValuePair<string, string>("password", pass));

            HTTP_Request.Content = new FormUrlEncodedContent(RequestData);

            var FormResponse = await HTTP_Client.SendAsync(HTTP_Request);


            if (FormResponse.IsSuccessStatusCode)
            {
                var Authentication_Response = await FormResponse.Content.ReadAsStringAsync();

                var Response_Tokens = JsonConvert.DeserializeObject<RootObject>(Authentication_Response);

                Store_Token(Response_Tokens.access_token);
                Foo.Dismiss();
                Toast.MakeText(context, "Login Successful!", ToastLength.Short).Show();
                context.StartActivity(typeof(DevList_Activity));
            }
            else
            {
                AlertDialog CredentialError = Bar.Create();
                CredentialError.SetTitle("Trouble processing request");
                CredentialError.SetMessage("Error Code: " + FormResponse.StatusCode.ToString());
                CredentialError.SetButton("OK", (c, ev) => { });
                Foo.Dismiss();
                CredentialError.Show();
            }
    }
        catch (HttpRequestException RequestFailed)
        {
            AlertDialog CredentialError = Bar.Create();
            CredentialError.SetTitle("Trouble logging in: request failed");
            CredentialError.SetMessage(RequestFailed.Message);
            CredentialError.SetButton("OK", (c, ev) => { });
            Foo.Dismiss();
            CredentialError.Show();
        }
        catch(Exception LoginFailed)
        {
            AlertDialog CredentialError = Bar.Create();
            CredentialError.SetTitle("Trouble logging in: login failed");
            CredentialError.SetMessage(LoginFailed.Message + "\n");
            CredentialError.SetButton("OK", (c, ev) => { });
            Foo.Dismiss();
            CredentialError.Show();
        }
    }