Google OAuth2:.NET中刷新和承载令牌的Exchange访问代码

时间:2015-07-15 10:48:41

标签: asp.net-mvc post oauth-2.0 google-oauth

我正在构建一个MVC应用程序,用户必须使用Google登录才能提供对其信息的离线访问。 我通过JavaScript从View获取Access代码并将其发送到控制器。 从控制器我向Google Api发出POST请求,但我一直收到错误请求400。

有没有人设法成功实现这个?

        string baseUri = "https://www.googleapis.com/oauth2/v3/token/";
        string parCode = string.Format("code={0}",Code);
        string parSecret = "client_secret=******";
        string parId = "client_id=*******************";
        string parRedirect = "redirect_uri=https://localhost:44301";
        string postData = string.Format("{0}&{1}&{2}&{3}", parCode, parRedirect, parId, parSecret);
        Uri postUri = new Uri(baseUri);
        ASCIIEncoding encoding = new ASCIIEncoding ();
        byte[] data = encoding.GetBytes (postData);


        var  request = (HttpWebRequest)WebRequest.Create(baseUri);
        request.Method = "POST";
        request.ContentType = " application/x-www-form-urlencoded";
        request.ContentLength = postData.Length;
        HttpWebResponse response;

        Stream newStream = request.GetRequestStream();
        newStream.Write(data, 0, data.Length);

0 个答案:

没有答案