英文:
Error 400 when requesting access token to auth2
问题
在C#中,使用3.5框架,我正在请求访问令牌来执行操作。
所以我创建了一个快速的桌面应用程序来测试我的类,一切都正常工作。
然后我将它移植到我的Web应用程序,但当我运行它时,出现错误400 - 错误的请求。
我检查了两次调用,它们是相同的。我漏掉了什么?
这是我的代码(我在(request.GetResponse()) 处获得错误):
public static string getAccessToken(string clientId, string clientSecret, string tenantId)
{
var authority = string.Format("https://login.microsoftonline.com/{0}/oauth2/token", tenantId);
var resource = "https://graph.microsoft.com";
var postData = string.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&resource={2}", clientId, clientSecret, resource);
var request = (HttpWebRequest)WebRequest.Create(authority);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
streamWriter.Write(postData);
streamWriter.Flush();
}
var response = (HttpWebResponse)request.GetResponse();
var responseStream = response.GetResponseStream();
var responseString = new StreamReader(responseStream).ReadToEnd();
tokenResponse tResponse = JsonConvert.DeserializeObject<tokenResponse>(responseString);
return tResponse.access_token;
}
我检查了来自Microsoft Graph的clientId、clientSecret和tenantId,它们都正确。
编辑:我创建了一个桌面应用程序。如果我从客户端运行它,它可以正常工作。但如果我从服务器运行它,就会出现错误。我应该检查什么?防火墙是打开的,这是一个Web服务器。
英文:
In C#, framwork 3.5, I'm requesting the access token to do things.
So I create a quick desktop app to test my class and everything works fine.
Then I move it to my web app, but when I run it i get Error 400 - Bad request.
I check and both calls are equal. What am I missing?
This is my code (i get the error ar (HttpWebResponse)request.GetResponse();):
public static string getAccessToken(string clientId, string clientSecret, string tenantId)
{
var authority = string.Format("https://login.microsoftonline.com/{0}/oauth2/token", tenantId);
var resource = "https://graph.microsoft.com";
var postData = string.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&resource={2}",clientId, clientSecret, resource);
var request = (HttpWebRequest)WebRequest.Create(authority);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
streamWriter.Write(postData);
streamWriter.Flush();
}
var response = (HttpWebResponse)request.GetResponse();
var responseStream = response.GetResponseStream();
var responseString = new StreamReader(responseStream).ReadToEnd();
tokenResponse tResponse = JsonConvert.DeserializeObject<tokenResponse>(responseString);
return tResponse.access_token;
}
I check clientId, clientSecret and tenantId from Microsoft Graph and they are ok.
EDIT: I create a desktop application. If I run it from my client, it works. If I run from the server, I get the error. What should I check? Firewall is open, it is a Web server
答案1
得分: 0
public class tokenResponse
{
public string resource { get; set; }
public string token_type { get; set; }
public int expires_in { get; set; }
public int ext_expires_in { get; set; }
public int expires_on { get; set; }
public int not_before { get; set; }
public string access_token { get; set; }
}
public static string getAccessToken(string clientId, string clientSecret, string tenantId)
{
string grant_type = "client_credentials";
string scope = "https://graph.microsoft.com/.default";
var uri = string.Format("https://login.microsoftonline.com/{0}/oauth2/v2.0/token", tenantId);
var postData = string.Format("client_id={0}&client_secret={1}&grant_type={2}&scope={3}", clientId, clientSecret, grant_type, scope);
var request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
streamWriter.Write(postData);
streamWriter.Flush();
}
var response = (HttpWebResponse)request.GetResponse();
var responseStream = response.GetResponseStream();
var responseString = new StreamReader(responseStream).ReadToEnd();
tokenResponse tResponse = JsonConvert.DeserializeObject<tokenResponse>(responseString);
return tResponse.access_token;
}
英文:
This works:
public class tokenResponse
{
public string resource { get; set; }
public string token_type { get; set; }
public int expires_in { get; set; }
public int ext_expires_in { get; set; }
public int expires_on { get; set; }
public int not_before { get; set; }
public string access_token { get; set; }
}
public static string getAccessToken(string clientId, string clientSecret, string tenantId)
{
string grant_type = "client_credentials";
string scope = "https://graph.microsoft.com/.default";
var uri = string.Format("https://login.microsoftonline.com/{0}/oauth2/v2.0/token", tenantId);
var postData = string.Format("client_id={0}&client_secret={1}&grant_type={2}&scope={3}", clientId, clientSecret, grant_type, scope);
var request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
streamWriter.Write(postData);
streamWriter.Flush();
}
var response = (HttpWebResponse)request.GetResponse();
var responseStream = response.GetResponseStream();
var responseString = new StreamReader(responseStream).ReadToEnd();
tokenResponse tResponse = JsonConvert.DeserializeObject<tokenResponse>(responseString);
return tResponse.access_token;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论