Android Question [SOLVED] Microsoft oAuth2 Login

josejad

Expert
Licensed User
Longtime User
Hi, from this post:

I want to try to login in my app with our work Microsoft account. I've seen no examples on the forum, so I will try to do it with your help.
I will try to follow this link from Microsoft.

After Register an application with the Microsoft identity platform I've started with Step 1: Request authorization.
With the help of copilot, I've translated this curl

B4X:
curl --location --request GET 'https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize?client_id=11111111-1111-1111-1111-111111111111&response_type=code&redirect_uri=https%3A%2F%2Flocalhost%2Fmyapp%2F&response_mode=query&scope=offline_access%20User.Read%20Mail.Read&state=12345'

To this B4X code. (I'm trying first in B4J, and I will later make the test in B4A)

B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private CLIENT_ID As String = "fadfasfas-5e92-323-83a6-adfadfafa" 'fake
    Private ClientSecret As String = "adfr223-c9432be-323-b972-aewe23r2" 'fake
    Private const REDIRECT_URI As String = "https://login.microsoftonline.com/common/oauth2/nativeclient"
    Private const AUTH_URL As String = "https://login.microsoftonline.com/my-tenant-id/oauth2/v2.0/authorize"
    Private const SCOPE As String = "offline_access%20User.Read%20Mail.Read"
    Private const STATE As String = "12345"
    Private btnLogin As Button
    Private WebView1 As WebView
End Sub

Public Sub Initialize
'    B4XPages.GetManager.LogEvents = True
End Sub

'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    Root.LoadLayout("MainPage")
End Sub

'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.




Sub btnLogin_Click
    Dim authUrl As String = AUTH_URL & "?client_id=" & CLIENT_ID & "&response_type=code" & "&redirect_uri=" & REDIRECT_URI & "&response_mode=query" & "&scope=" & SCOPE & "&state=" & STATE
    WebView1.LoadUrl(authUrl)
End Sub

Sub WebView1_PageFinished (Url As String)
    If Url.StartsWith(REDIRECT_URI) Then
        Dim code As String = Url.SubString2(Url.IndexOf("code=") + 5, Url.Length)
        Log("Authorization Code: " & code)
        'Here you can continue getting the access token
    End If
End Sub

If I run the above code (attached project, change your id's in the global variables), I'm asked in the webview for my user, password and F2A, but after that, I get an message saying I need the approval from an admin.

Probably I need to contact my IT department to get that. Anyway, if I press in the "continue without appoval" I get the message: "AADSTS90014: The required field 'request' is missing from the credential. Ensure that you have all the necessary parameters for the login request."

@andymc, can you test the above code to see if you get something else? Or can you share the code where you login with Microsoft?


Thanks in advance.
 

Attachments

  • MSoAuth2.zip
    9.1 KB · Views: 34
Last edited:

josejad

Expert
Licensed User
Longtime User
Hi Alexander:

Thanks for the hint. I haven't enabled it, but searching, it seems to be enabled by default:

Configured permissions
Permission Name/APITypeDescriptionAdministrator's consent required
Microsoft Graph (1)
User.ReadDelegatedLogin and read user's profileNo
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Upvote 0

josejad

Expert
Licensed User
Longtime User
Now I have the permission granted.

And I've found a working code to log, get my data and my profile pic!!


You can use:

 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…