我正在尝试检索oauth访问令牌以在asp.net mvc中调用某些Google Apis,并且我为操作编写了以下代码:
public ActionResult GetoAuthToken() { String url = "https://accounts.google.com/o/oauth2/token"; // Create a request using a URL that can receive a post. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); // Set the Method property of the request to POST. request.Method = "POST"; request.Host = "accounts.google.com"; // Create POST data and convert it to a byte array. string postData = String.Format("code={0}&client_id={1}&client_secret={2}&redirect_uri={3}&grant_type=authorization_code",Request.QueryString["code"].ToString(),OAuthConfig.client_id,OAuthConfig.client_secret,OAuthConfig.token_redirect_uri); ASCIIEncoding encoding = new ASCIIEncoding(); byte[] byteArray = encoding.GetBytes(postData); // Set the ContentType property of the WebRequest. request.ContentType = "application/x-www-form-urlencoded"; // Set the ContentLength property of the WebRequest. request.ContentLength = byteArray.Length; // Get the request stream. Stream dataStream = request.GetRequestStream(); // Write the data to the request stream. dataStream.Write(byteArray,byteArray.Length); // Close the Stream object. dataStream.Close(); // Get the response. WebResponse response = request.GetResponse(); // SOME CODE TO PROCESS THE RESPONSE Response.Redirect("/Home"); return View(); }
解决方法
谷歌确实提供了一个更高级别的库来处理它的服务,它处理网址的格式,我发现它可以更容易地使用它们的api.见
http://code.google.com/p/google-api-dotnet-client/
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。