Access token Google+

依然范特西╮ 提交于 2019-12-03 21:58:16
e-MEE

You forgot the POST request. Try this:

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 ...

This will give you a Json with the access token. Also you can see my question, that I asked today and solved later.

The Google+ API uses OAuth 2.0 for authorization. You seem to be implementing a mix of OAuth 2.0 and OAuth 1.0: your code calculates an OAuth 1.0 oauth_signature for the OAuth 2.0 request which is obsolete in OAuth 2.0.

Take a look at the OAuth 2.0 specification draft and try to follow the example in Google's OAuth 2.0 documentation exactly. Or just use Google's .NET library which comes with OAuth 2.0 support.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!