Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 谷歌日历oAuth错误请求_C#_Asp.net_Rest_Oauth_Google Oauth - Fatal编程技术网

C# 谷歌日历oAuth错误请求

C# 谷歌日历oAuth错误请求,c#,asp.net,rest,oauth,google-oauth,C#,Asp.net,Rest,Oauth,Google Oauth,我正试图通过谷歌的oAuth(v2)获得一个访问令牌。我正在使用ASP.NET4.0Web表单(C#)。我遇到的问题是,当我提交HttpWebRequest时,我收到一个错误的请求。我正在使用我在这里找到的代码()。这是我在pageLoad事件中的代码: string strCode = ""; string strClientID = "********.apps.googleusercontent.com"; string strCli

我正试图通过谷歌的oAuth(v2)获得一个访问令牌。我正在使用ASP.NET4.0Web表单(C#)。我遇到的问题是,当我提交HttpWebRequest时,我收到一个错误的请求。我正在使用我在这里找到的代码()。这是我在pageLoad事件中的代码:

            string strCode = "";
        string strClientID = "********.apps.googleusercontent.com";
        string strClientSecret = "**********";
        string strRedirectURI = "http://www.example.com";

        if (Request.QueryString["code"] != null && Request.QueryString["code"].ToString() != "")
        {
            strCode = Request.QueryString["code"].ToString();
            StreamWriter sw = new StreamWriter(Server.MapPath("~\\") + "code.txt");
            sw.WriteLine(Request.QueryString["code"].ToString());
            sw.Close();


            string queryStringFormat = "code={0}&redirect_uri={1}&client_id={2}&client_secret={3}&grant_type=authorization_code";
            string postcontents = string.Format(queryStringFormat
                                               , HttpUtility.UrlEncode(strCode)
                                               , HttpUtility.UrlEncode(strClientID)
                                               , HttpUtility.UrlEncode(strClientSecret)
                                               , HttpUtility.UrlEncode(strRedirectURI));
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://accounts.google.com/o/oauth2/token");
            request.Method = "POST";
            byte[] postcontentsArray = Encoding.UTF8.GetBytes(postcontents);
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = postcontentsArray.Length;
            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(postcontentsArray, 0, postcontentsArray.Length);
                requestStream.Close();
                WebResponse response = request.GetResponse();//Error Happens Here
                using (Stream responseStream = response.GetResponseStream())
                using (StreamReader reader = new StreamReader(responseStream))
                {
                    string responseFromServer = reader.ReadToEnd();
                    reader.Close();
                    responseStream.Close();
                    response.Close();
                    //return SerializeToken(responseFromServer);
                }
            }
        }

感谢您的帮助。谢谢。

乍一看,代码看起来基本正确,但我认为您的
字符串.Format
参数的顺序与
queryStringFormat
不同:

string queryStringFormat = "code={0}&redirect_uri={1}&client_id={2}&client_secret={3}&grant_type=authorization_code";
string postcontents = string.Format(queryStringFormat
                       , HttpUtility.UrlEncode(strCode)
                       // Redirect URI should be here (or switch the order above)
                       , HttpUtility.UrlEncode(strClientID)
                       , HttpUtility.UrlEncode(strClientSecret)
                       , HttpUtility.UrlEncode(strRedirectURI));