Management API Access Tokens for Production returns access denied

Hi i was following the below link " Get Management API Access Tokens for Production "

var client = new RestClient(“https://YOURDOMAIN/oauth/token”);
var request = new RestRequest(Method.POST);
request.AddHeader(“content-type”, “application/x-www-form-urlencoded”);
request.AddParameter(“application/x-www-form-urlencoded”, “grant_type=client_credentials&client_id=%24%7Baccount.clientId%7D&client_secret=YOUR_CLIENT_SECRET&audience=https%3A%2F%2F%24%7Baccount.namespace%7D%2Fapi%2Fv2%2F”, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

I tried to get the token from a console app.
below is the error I get
Error : {“error”:“access_denied”,“error_description”:“Unauthorized”}

when i try from fiddler sometimes it gives me the same error or other wise it is 410 Gone.

Hi @modish10,

Your request looks good. Just to confirm, is your application a machine-to-machine (M2M) application that has been authorized to use your tenant’s management API?

After a failed request, do you see an associated tenant log (from your Auth0 dashboard, Monitorig > Logs)? The logs will sometimes provide additional information which indicate what went wrong with the request.

You could also try testing from your terminal using cURL using the commands in the guide to see if you still get an error.

Yes M2M

From Postman its working fine.
it doesn’t show any log.

So I changed the code as below, and it worked.

private static async Task<string> PostFormUrlEncoded (string url, IEnumerable<KeyValuePair<string, string>> postData){
            using(var httpClient = new HttpClient()){
                using(var content = new FormUrlEncodedContent(postData)) {
                    content.Headers.Clear();
                    content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                    HttpResponseMessage response = await httpClient.PostAsync(url, content);
                    return await response.Content.ReadAsStringAsync();
                }
            }
        }

 private static void GetToken() {
            IEnumerable<KeyValuePair<string, string>> postData = new List<KeyValuePair<string, string>>{
                new KeyValuePair<string, string>("grant_type", "client_credentials"),
                new KeyValuePair<string, string>("client_id", "YOUR CLIENT_ID"),
                new KeyValuePair<string, string>("client_secret", "YOUR CLIENT_SECRET"),
                new KeyValuePair<string, string>("audience", "https://YOURDOMAIN/api/v2/")
            };
            var tempT = Task.Run(async () => await     PostFormUrlEncoded("https://YourDomain/oauth/token", postData))
                    .Result;
}
2 Likes

That’s great! Glad it is working for you now.

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.