Is it possible to generate azure authorization code from application code without browser redirection? I've credentials that are needed to generate the auth code that is needed to generate access token. Is there a way to input the credentials via code and get authorization code using C# code and read it to a string variable?
https://login.microsoftonline.com/<tenant-id>/oauth2/v2.0/authorize?client_id=<client-id>
&response_type=code
&redirect_uri=<redirect-uri>
&response_mode=query
&scope=2ff814a6-3304-4ab8-85cb-cd0e6f879c1d%2F.default
&state=<state>
Need to pass the authorization code generate in the above code to get the access token.
If anyone can help, I would be grateful.
I tried to reproduce the same in my environment and got the below results:
When I tried to generate the Authorization code by passing the username and password parameters it still asked me to sign-in to the account:
Note that: Generating Authorization Code used Authorization Code Grant Flow which requires user-interaction. A user-agent that permits redirection from the authorization server back to your application is necessary for the auth code flow.
So, by design, when you hit the authorize URL it will be redirected to sign-in and code will be generated:
Alternatively, to access the SharePoint you can grant required Application
Api permissions and generate access token via Client Credential Flow.
https://login.microsoftonline.com/TenantID/oauth2/v2.0/token
client_id:ClientID
client_secret:ClientSecet
scope:scope
grant_type:client_credentials
If Client Credential Flow doesn't achieve your scenario, then you can make use of ROPC flow (Not recommended by Microsoft) which doesn't redirect to login as mentioned by juunas.
Reference:
Microsoft identity platform and OAuth 2.0 authorization code flow