I have a postman request that works correctly in obtaining a bearer token, that I can grab and use to make a successful request. But I cannot obtain an equally valid bearer token in c# and I have tried about 100 different approaches.
Here is the Postman request, which gets a valid bearer token:
Scope: AX.FullAccess CustomService.FullAccess Odata.FullAccess
Auth URL: https://login.microsoftonline.com/abe3ad26.../oauth2/authorize?resource=https://myCust.sandbox.operations.dynamics.com/
In the Client Authentication
pulldown there are 2 options. The other option is Send as basic auth header
. It doesn't matter which I choose, they both work.
And here is my non-working c# attempting to do the same thing postman does:
using(WebClient client = new WebClient()) {
var querystring = new System.Collections.Specialized.NameValueCollection();
querystring.Add("grant_type", "authorization_code");
querystring.Add("client_id", "e93c4014...");
querystring.Add("client_secret", "USu8Q...");
querystring.Add("redirect_uri", "https://myCust.sandbox.operations.dynamics.com/");
querystring.Add("resource", "https://myCust.sandbox.operations.dynamics.com/");
querystring.Add("scope", "AX.FullAccess CustomService.FullAccess Odata.FullAccess");
byte[] responsebytes = client.UploadValues("https://login.microsoftonline.com/abe3ad26.../oauth2/authorize", "POST", querystring);
[code to retrieve the response...]
}
In all the various permutations I have tried of the above code, I either get an exception with no message, or I get a large html response that is merely Microsoft's generic page titled Sign in to your account
.
I'm unsure whether resource
should be a formal param, or appended to the Auth URL
as in postman, or what.
I am certain the client_id
and client_secret
are right, both in name
and value
. As for all the other params, I'm not as sure. I'm especially unsure what exact url I should be POSTing this request to. I have tried the full Auth URL
with and without the resource querystring value appended, no luck either way.