My app is in Flutter/Dart. I am trying to generate an access token through OAuth2 with Azure credentials. Once I login with my credentials, it successfully authorizes, but it doesn't redirect automatically to the app, and when I exit out of page to go back to the app, it prints the token to be null.
Code:
OAuth2Client client = OAuth2Client(
redirectUri: 'https://oauth.pstmn.io/v1/browser-callback',
customUriScheme: '${customUri}',
authorizeUrl: 'https://login.microsoftonline.com/${tenantID}/oauth2/authorize',
tokenUrl: 'https://login.microsoftonline.com/${tenantID}/oauth2/token',
);
try{
AccessTokenResponse tknResp = await client.getTokenWithAuthCodeFlow(
clientId: '${clientID}',
clientSecret: '${clientSecret}',
scopes: ['api://${tenantID}/Users.Login'],
);
print("TOKEN IS: ${await tknResp.tokenType} ${await tknResp.scope} ${await tknResp.accessToken}");
}catch(e){
print("TOKEN ERROR IS: ${e}");
}
Note: I run it through it Android Studio
Thank you!
I tried running the code above, but I received Null as the Access Token.
There is an alternate approach for your Azure auth problem. this one doesn't use OAuth though.
azure_silent_auth
Try this package. but you need to know the client-id, tenant-id and you need to add http://localhost:3000 redirect URI to the azure app authorization on azure portal.
This package has solution for one of your issue. i,e
not redirecting back to the application after authentication. use thePCAuthenticatorinstead of theDefaultAuthenticatorprovided in the example.PCAuthenticatorcreates a separate window for loading the auth screen instead of redirecting it to the browser. also this new window is closed after authentication is done. you can modify this and create your own custom authenticator and use it inAzureAuthconstructor too if you wish.as far as your second problem, after successful login you can get the access token using
AzureAuth->getAccessTokenwhen ever and where ever you want. it even refresh the token for you if it is expired.This package by default saves the access token securely, so that you can do a silent login the next time using
AzureAuth->silentLogin.Hope this helps.