I have generated Access Token through OAuth by authenticating with admin account then trying to fetch all the labels with use of Gmail API (https://developers.google.com/apis-explorer/#p/gmail/v1/gmail.users.labels.list) for other user in same domain. But facing issue with an error : Delegation denied for [email protected].
Below is the code:
string uri = "https://www.googleapis.com/oauth2/v3/token";
string results = string.Empty;
string responseString = null;
using (var clientForToken = new HttpClient())
{
var values = new List<KeyValuePair<string, string>>();
values.Add(new KeyValuePair<string, string>("grant_type", "authorization_code"));
values.Add(new KeyValuePair<string, string>("code", code));
//values.Add(new KeyValuePair<string, string>("sub", "[email protected]"));
values.Add(new KeyValuePair<string, string>("client_id", Convert.ToString(ConfigurationManager.AppSettings["ida:ClientId"])));
values.Add(new KeyValuePair<string, string>("client_secret", Convert.ToString(ConfigurationManager.AppSettings["ida:ClientSecret"])));
values.Add(new KeyValuePair<string, string>("redirect_uri", "http://localhost:6402/Home/ClaimExchangeServerAccessToken"));
var content = new FormUrlEncodedContent(values);
var response = clientForToken.PostAsync(uri, content).Result;
responseString = response.Content.ReadAsStringAsync().Result;
}
var newToken = AccessTokenFromJson(responseString);
//Uri requestUri = new Uri("https://www.googleapis.com/admin/directory/v1/users?domain=mydomain.com");
Uri requestUri = new Uri("https://www.googleapis.com/gmail/v1/users/[email protected]/labels");
var httpRequest = new HttpRequestMessage()
{
RequestUri = requestUri,
Method = HttpMethod.Get,
};
httpRequest.Headers.TryAddWithoutValidation("Authorization", string.Format("Bearer {0}", newToken));
//httpRequest.Headers.TryAddWithoutValidation("sub", "[email protected]");
var clientHandler = new HttpClientHandler()
{
AutomaticDecompression = System.Net.DecompressionMethods.None
};
var client = new HttpClient(clientHandler);
HttpResponseMessage responseMessage = null;
responseMessage = client.SendAsync(httpRequest).Result;
Stream receiveStream = responseMessage.Content.ReadAsStreamAsync().Result;
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
string data = readStream.ReadToEnd();
But getting the below error response :
{
"error": {
"errors": [
{
"domain": "global",
"reason": "forbidden",
"message": "Delegation denied for [email protected]"
}
],
"code": 403,
"message": "Delegation denied for [email protected]"
}
}
Please help me out somebody.