Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在windows应用程序C#.net中获取Google plus访问令牌_C#_Google Api_Google Plus_Access Token_Bad Request - Fatal编程技术网

如何在windows应用程序C#.net中获取Google plus访问令牌

如何在windows应用程序C#.net中获取Google plus访问令牌,c#,google-api,google-plus,access-token,bad-request,C#,Google Api,Google Plus,Access Token,Bad Request,我正在准备一个windows应用程序,因为我想使用oAuth2从google plus访问信息 但是我收到了对我的以下代码的错误请求。。我能够在谷歌控制台中创建应用程序,并获取应用程序访问的“代码” WebRequest request = WebRequest.Create( GoogleAuthenticationServer.Description.TokenEndpoint ); // You must use POST for the code exchang

我正在准备一个windows应用程序,因为我想使用oAuth2从google plus访问信息

但是我收到了对我的以下代码的错误请求。。我能够在谷歌控制台中创建应用程序,并获取应用程序访问的“代码”

WebRequest request = WebRequest.Create(
    GoogleAuthenticationServer.Description.TokenEndpoint
);

        // You must use POST for the code exchange.
        request.Method = "POST";

        // Create POST data.
        string postData = FormPostData(code);
        byte[] byteArray = Encoding.UTF8.`enter code here`GetBytes(postData);

        // Set up the POST request for the code exchange.
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = byteArray.Length;
        Stream dataStream = request.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();

        // Perform the POST and retrieve the server response with
        // the access token and/or the refresh token.
        WebResponse response = request.GetResponse();
        dataStream = response.GetResponseStream();
        StreamReader reader = new StreamReader(dataStream);
        string responseFromServer = reader.ReadToEnd();
        reader.Close();
        dataStream.Close();
        response.Close();

        // Convert the response JSON to an object and return it.
        return JsonConvert.DeserializeObject<OAuthResponseObject>(
            responseFromServer);
WebRequest-request=WebRequest.Create(
GoogleAuthenticationServer.Description.TokenEndpoint
);
//您必须使用POST进行代码交换。
request.Method=“POST”;
//创建POST数据。
字符串postData=FormPostData(代码);
byte[]byteArray=Encoding.UTF8.`enter code here`GetBytes(postData);
//设置代码交换的POST请求。
request.ContentType=“application/x-www-form-urlencoded”;
request.ContentLength=byteArray.Length;
Stream dataStream=request.GetRequestStream();
写入(byteArray,0,byteArray.Length);
dataStream.Close();
//使用执行POST并检索服务器响应
//访问令牌和/或刷新令牌。
WebResponse=request.GetResponse();
dataStream=response.GetResponseStream();
StreamReader=新的StreamReader(数据流);
字符串responseFromServer=reader.ReadToEnd();
reader.Close();
dataStream.Close();
response.Close();
//将响应JSON转换为对象并返回它。
返回JsonConvert.DeserializeObject(
响应服务器);
现在,我正在尝试使用该代码获取访问令牌。这给了我不好的要求

我也关注了一些关于stackoverflow的帖子。但是没有一个是为我工作的

谢谢

编辑:


谢谢你的回复。我自己也能做到:)

我想你可以这样开始:


对于web应用程序,我强烈建议您使用中演示的一次性代码流。请尝试按照快速入门说明进行操作,以确保没有遗漏任何步骤。当您以这种方式进行Google+登录时,您将能够通过无线方式安装Android应用程序(如果您的站点有一个应用程序),并将应用最佳做法进行授权

示例中提供了所有执行此操作的代码,还演示了与Google客户端库的集成-这打开了对所有Google API和产品集成的访问

从Windows应用程序或已安装的应用程序中,您需要自己做更多的繁重工作。以下博客文章介绍了如何在遗留方案中进行授权:

还有一个例子:

几点注意:

  • 创建客户端ID时,请确保该ID用于已安装的应用程序
  • 授权码取自用户授权后的窗口标题;这有点狡猾,你应该在你的应用程序中宿主的窗口中进行
  • 以这种方式执行授权将不允许您在Android上进行空中安装。为了做到这一点,您可以在应用程序内部托管一个webview,并将其用于一次性代码流,但我从未见过它在Windows中工作

  • 我可以用下面的代码自己做

        private void button2_Click(object sender, EventArgs e)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/o/oauth2/token");
            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.Method = "POST";
            authLink.AppendFormat("code={0}", code);
            authLink.AppendFormat("&client_id={0}", "996688211762.apps.googleusercontent.com");
            authLink.AppendFormat("&client_secret={0}", "nprfJuBUOyU2hsb3tqt1XDnB");
            authLink.AppendFormat("&redirect_uri={0}", "urn:ietf:wg:oauth:2.0:oob");
            authLink.Append("&grant_type=authorization_code");
            UTF8Encoding utfenc = new UTF8Encoding();
            byte[] bytes = utfenc.GetBytes(authLink.ToString());
            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
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
            try // get the response
            {
                HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
                if (webResponse == null) { MessageBox.Show("null"); }
                StreamReader sr = new StreamReader(webResponse.GetResponseStream());
                textBox1.Text = sr.ReadToEnd().Trim();
                //MessageBox.Show(sr.ReadToEnd().Trim());
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            StringBuilder authLink = new StringBuilder();
            authLink.Append("https://accounts.google.com/o/oauth2/auth");
            authLink.AppendFormat("?scope={0}", "https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile");
            authLink.AppendFormat("&client_id={0}", "xxxxxx.apps.googleusercontent.com");
            authLink.AppendFormat("&redirect_uri={0}", "urn:ietf:wg:oauth:2.0:oob");
            authLink.Append("&response_type=code");
            string u = @"https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&client_id=xxxxxxxx.apps.googleusercontent.com";
            webBrowser1.Navigate(u);
        }
    

    我假设窗户上有两个按钮。。button1用于从google获取代码,button2使用该代码获取访问令牌,这正是我所寻找的。

    这不是访问令牌的最佳方式。是否有针对桌面应用程序实现的示例。。不是web应用程序?嘿,谢谢你的回答。那么,我们应该为自己的项目修改哪些参数呢?只有应用程序id和客户端机密?我想你不能用这个让google+post进入你的流?