Forum Discussion
How to make source code from CQDPowerShell cmdlet acquiring access token non-interactively
Please find the code here in gitrepo.
here we used loop the access code to generate each time for the requirement.
Token1, Token2, Token3 will generate in each request.
Please find the code for getting Refresh token. Use this function to call each time to retrieve or get the access token.
Please check above solutions and let us know if you need any thing.
- Oleksii_KolinkoApr 24, 2020Copper Contributor
First example works with client secret, which in this case is not availible.
While according to seconds example I written the code, which should theoretically return access token out of "authorization code":
using (HttpClient httpClient = new HttpClient()) { string newRequestPath = $"https://login.microsoftonline.com/<tenant guid>/oauth2/v2.0/token"; string requestBOdy = $"grant_type=authorization_code&client_id={clientId}&redirect_uri={redirectUrl}&code={code}"; HttpContent httpContent = new StringContent(requestBOdy, Encoding.UTF8, "application/x-www-form-urlencoded"); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, newRequestPath); request.Content = httpContent; Task<HttpResponseMessage> response = httpClient.SendAsync(request); response.Wait(); Task<string> responseString = response.Result.Content.ReadAsStringAsync(); responseString.Wait(); }
But it returns the following error message:
"{\"error\":\"invalid_client\",\"error_description\":\"AADSTS7000218: The request body must contain the following parameter: 'client_assertion' or 'client_secret'.\\r\\nTrace ID: d89e74e1-36dd-4807-9c51-dda7003c0700\\r\\nCorrelation ID: eb4c20b0-7ab5-41c4-a7b3-3d793e6de0b6\\r\\nTimestamp: 2020-04-24 12:07:16Z\",\"error_codes\":[7000218],\"timestamp\":\"2020-04-24 12:07:16Z\",\"trace_id\":\"d89e74e1-36dd-4807-9c51-dda7003c0700\",\"correlation_id\":\"eb4c20b0-7ab5-41c4-a7b3-3d793e6de0b6\",\"error_uri\":\"https://login.microsoftonline.com/error?code=7000218\"}"