gpt4 book ai didi

c# - 从代码中获取来自 google oauth 的访问 token

转载 作者:行者123 更新时间:2023-11-30 18:35:01 26 4
gpt4 key购买 nike

我使用下面的代码从下面的代码中获取访问 token

String code = HttpContext.Current.Request["code"];
string redirecturl = HttpContext.Current.Request["url"];

string Url = "https://accounts.google.com/o/oauth2/token";
string grant_type = "authorization_code";
string redirect_uri_encode = UrlEncodeForGoogle(url);
string data = "code={0}&client_id={1}&client_secret={2}&redirect_uri={3}&grant_type={4}&access_type={5}";

HttpWebRequest request = HttpWebRequest.Create(Url) as HttpWebRequest;
string result = null;
request.Method = "POST";
request.KeepAlive = true;
request.ContentType = "application/x-www-form-urlencoded";
string param = string.Format(data, code,configurationInfo.oauthclientid , configurationInfo.oauthclientsecretid, redirect_uri_encode, grant_type, "offline");

var bs = Encoding.UTF8.GetBytes(param);
using (Stream reqStream = request.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
}

using (WebResponse response = request.GetResponse())
{
var sr = new StreamReader(response.GetResponseStream());
result = sr.ReadToEnd();
sr.Close();

}

我得到的回应是

 The remote server returned an error: (400) Bad Request.

我不知道哪里错了

等待您的宝贵意见

最佳答案

Google 还提供了用于访问其服务的更高级别的库。我发现它使使用其 API 变得更加容易。

http://code.google.com/p/google-api-dotnet-client/

关于c# - 从代码中获取来自 google oauth 的访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15573245/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com