Error 400 when requesting access token to auth2

huangapple go评论53阅读模式
英文:

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(&quot;https://login.microsoftonline.com/{0}/oauth2/token&quot;, tenantId);
            var resource = &quot;https://graph.microsoft.com&quot;;
            var postData = string.Format(&quot;grant_type=client_credentials&amp;client_id={0}&amp;client_secret={1}&amp;resource={2}&quot;,clientId, clientSecret, resource);
            var request = (HttpWebRequest)WebRequest.Create(authority);

            request.Method = &quot;POST&quot;;
            request.ContentType = &quot;application/x-www-form-urlencoded&quot;;

            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&lt;tokenResponse&gt;(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}&amp;client_secret={1}&amp;grant_type={2}&amp;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 = &quot;client_credentials&quot;;
	string scope = &quot;https://graph.microsoft.com/.default&quot;;
	
	var uri = string.Format(&quot;https://login.microsoftonline.com/{0}/oauth2/v2.0/token&quot;, tenantId);
	var postData = string.Format(&quot;client_id={0}&amp;client_secret={1}&amp;grant_type={2}&amp;scope={3}&quot;, clientId, clientSecret, grant_type, scope);
	var request = (HttpWebRequest)WebRequest.Create(uri);

	request.Method = &quot;POST&quot;;
	request.ContentType = &quot;application/x-www-form-urlencoded&quot;;

	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&lt;tokenResponse&gt;(responseString);
	return tResponse.access_token;
}

huangapple
  • 本文由 发表于 2023年2月23日 19:17:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75544092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定