c# - 访问 token Google+

标签 c# api oauth google-api google-plus

我在使用新发布的 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/

相关文章:

c# - 非嵌入资源js文件可以访问资源文件(.resx)吗?

c# - 使用 MSBUILD API 更新 *.CSPROJ

api - 是否有工具可以在文档中定义 Rest API?

django - Google OAUTH 给出 502 错误

c# - 如何显示 DataAnnotations 的错误消息

c# - 当 typeName 表示大小无效的数组类型时,Type.GetType() 抛出

Java:如何集成其他软件

php - 如何在发送之前更改 SOAP 请求中的 namespace 前缀(皇家邮政 - 运送请求)

spring-security - Spring Security 5 OAuth 2 社交注销

java - 错误由: java. lang.ClassNotFoundException : org. springframework.security.context.DelegatingApplicationListener引起