C# 请确认它是url编码的正文。请检查您发布的json数据。这不是您在代码中发送的重定向uri,它们必须相同。根据链接,我已将重定向uri更改为“urn:ietf:wg:oauth:2.0:oob”。另外,我发现数据不是url编码的,它必须是json。因此,使

C# 请确认它是url编码的正文。请检查您发布的json数据。这不是您在代码中发送的重定向uri,它们必须相同。根据链接,我已将重定向uri更改为“urn:ietf:wg:oauth:2.0:oob”。另外,我发现数据不是url编码的,它必须是json。因此,使,c#,google-drive-api,httpwebrequest,C#,Google Drive Api,Httpwebrequest,请确认它是url编码的正文。请检查您发布的json数据。这不是您在代码中发送的重定向uri,它们必须相同。根据链接,我已将重定向uri更改为“urn:ietf:wg:oauth:2.0:oob”。另外,我发现数据不是url编码的,它必须是json。因此,使用newtonsoft.json重新打包代码,生成以下输出。“{”client\u code\:“4/sZ7URBc5qJ6vYt5t3dLz\u xyZH-r-ptZdxCZTkx9hdYo\”,“client\u id\”:“7559332


请确认它是url编码的正文。请检查您发布的json数据。这不是您在代码中发送的重定向uri,它们必须相同。根据链接,我已将重定向uri更改为“urn:ietf:wg:oauth:2.0:oob”。另外,我发现数据不是url编码的,它必须是json。因此,使用newtonsoft.json重新打包代码,生成以下输出。“{”client\u code\:“4/sZ7URBc5qJ6vYt5t3dLz\u xyZH-r-ptZdxCZTkx9hdYo\”,“client\u id\”:“755933294823-t0afvat7k75c49io03o8c0noer7m9.apps.googleusercontent.com\,“client\u secret\”:“9e1jlc1tawra4xzzxxt2h2h\,“redirect\uURI:”OAuth2w:Outh\,“grant.com\”类型:“:”grant\”grant授权代码\“}”。但还是一样的问题@戴姆托。400个错误的请求。我遗漏了什么吗?你不是在使用json文件吗?你是在用一种很难做到的方式。阅读我发送给你的第一个链接,它将引导你了解每个请求的外观,如果你在我的网站上停留足够长的时间,你可能会找到一篇关于如何做到这一点的帖子。我在某个地方有一篇关于这一点的很老的帖子。
{"installed":{"client_id":"xxxx","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://accounts.google.com/o/oauth2/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"xxxx","redirect_uris":["urn:ietf:wg:oauth:2.0:oob","http://localhost"]}} 
POST /oauth2/v3/token HTTP/1.1
Host: www.googleapis.com
Content-Type: application/x-www-form-urlencoded
        string tokenUri = @"https://accounts.google.com/o/oauth2/token";
        HttpWebRequest request=(HttpWebRequest) WebRequest.Create(tokenUri);


        NameValueCollection outgoingQueryString = HttpUtility.ParseQueryString(String.Empty);
        outgoingQueryString.Add("code", this.clientCode);
        outgoingQueryString.Add("client_id", this.clientID);
        outgoingQueryString.Add("client_secret", this.clientSecret);
        outgoingQueryString.Add("redirect_uri", "https://oauth2-login-demo.appspot.com/code");
        outgoingQueryString.Add("grant_type","authorization_code");
        string postdata = outgoingQueryString.ToString();


        byte[] byteArray = Encoding.UTF8.GetBytes(postdata);

        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = byteArray.Length;

        Stream reqStr = request.GetRequestStream();
        reqStr.Write(byteArray, 0, byteArray.Length);
        reqStr.Flush();
        reqStr.Close();


        HttpWebResponse response=request.GetResponse() as HttpWebResponse;
        Console.WriteLine(response.StatusCode.ToString());
string tokenUri = "https://accounts.google.com/o/oauth2/token";
        TokenFileds f = new TokenFileds() { client_code = this.clientCode, client_id = this.clientID, client_secret = this.clientSecret, redirect_uri = "urn:ietf:wg:oauth:2.0:oob", grant_type = "authorization_code" };
        //string retString=this.SerializeToJson<TokenFileds>(f);
        string retString = this.NewjsonLib(f);
        byte[] byteArray=Encoding.UTF8.GetBytes(retString);

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(tokenUri);
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
        request.ContentLength = byteArray.Length;

        Stream strm = request.GetRequestStream();
        strm.Write(byteArray, 0, byteArray.Length);
        strm.Flush();
        strm.Close();
        HttpWebResponse response =request.GetResponse() as HttpWebResponse;