gpt4 book ai didi

c# - 访问 token Google+

转载 作者:行者123 更新时间:2023-11-30 22:35:13 24 4
gpt4 key购买 nike

我在使用新发布的 Google+ API 检索访问 token 时遇到了一些问题...

我一直在关注 documentation ,我得到了一个代码(“4/blablabla”)但是当我发送 POST 请求以获取访问 token 时,我得到一个“(400)错误请求”响应......

这是我的一段代码:

// We have a request code, now we want an access token
StringBuilder authLink = new StringBuilder();
authLink.Append("https://accounts.google.com/o/oauth2/token");
authLink.AppendFormat("?code={0}", gplusCode);
authLink.AppendFormat("&client_id={0}", myClientId);
authLink.AppendFormat("&client_secret={0}", mySecret);
authLink.AppendFormat("&redirect_uri={0}", myRedirectUri);
authLink.Append("&scope=https://www.googleapis.com/auth/plus.me");
authLink.Append("&grant_type=authorization_code");

OAuthBase oAuth = new OAuthBase();
string normalizedUrl, normalizedRequestParameters;
string oAuthSignature = oAuth.GenerateSignature(new Uri(authLink.ToString()), appKey, appSecret, code, null, HttpMethod.POST.ToString(), oAuth.GenerateTimeStamp(), oAuth.GenerateNonce(), OAuthBase.SignatureTypes.HMACSHA1, out normalizedUrl, out normalizedRequestParameters);
oAuthSignature = oAuth.UrlEncode(oAuthSignature);

// Rebuild query url with authorization
string url = string.Format("{0}?{1}&oauth_signature={2}", normalizedUrl, normalizedRequestParameters, oAuthSignature);

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url.ToString());
request.Method = HttpMethod.POST.ToString();
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = 0;

// Send the request and get the response
try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
// Do stuff ...

最佳答案

您忘记了 POST 请求。试试这个:

string url = "https://accounts.google.com/o/oauth2/token";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url.ToString());
request.Method = HttpMethod.POST.ToString();
request.ContentType = "application/x-www-form-urlencoded";

// You mus do the POST request before getting any response
UTF8Encoding utfenc = new UTF8Encoding();
byte[] bytes = utfenc.GetBytes(parameters); // parameters="code=...&client_id=...";
Stream os = null;
try // send the post
{
webRequest.ContentLength = bytes.Length; // Count bytes to send
os = webRequest.GetRequestStream();
os.Write(bytes, 0, bytes.Length); // Send it
}

try
{
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
// Do stuff ...

这将为您提供一个带有访问 token 的 Json。你也可以看到my question ,我今天问了,后来解决了。

关于c# - 访问 token Google+,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7501938/

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