【问题标题】:How to generate refresh and access token for Windows phone app如何为 Windows 手机应用程序生成刷新和访问令牌
【发布时间】:2018-10-25 17:13:28
【问题描述】:

我有一个 windows phone 8 应用程序,我正在尝试进行 Google 身份验证。 我进入登录页面,登录后将我带到同意页面。 单击允许访问后,我没有得到访问令牌和刷新令牌作为响应。

我得到的回应如下:

{
"error" : "invalid_request",
"error_description" : "Missing header: Content-Type"
}

StatusCode 是错误请求

这是我的代码:

private void webBrowserGooglePlusLogin_Navigating(object sender, NavigatingEventArgs e)
    {

        if (e.Uri.Host.Equals("localhost"))
        {
            webBrowserGooglePlusLogin.Visibility = Visibility.Collapsed;
            e.Cancel = true;
            int pos = e.Uri.Query.IndexOf("=");              
            code = pos > -1 ? e.Uri.Query.Substring(pos + 1) : null;
        }
        if (string.IsNullOrEmpty(code))
        {
           // OnAuthenticationFailed();
        }
        else
        {
            var request = new RestRequest(this.TokenEndPoint, Method.POST);
            request.AddParameter("code", code);
            request.AddParameter("client_id", this.ClientId);
            request.AddParameter("client_secret", this.Secret);
            request.AddParameter("redirect_uri", "http://localhost");
            request.AddParameter("grant_type", "authorization_code");
            //request.AddHeader("Content-type", "json");

            client.ExecuteAsync<AuthResult>(request, GetAccessToken);
        }
    }

    void GetAccessToken(IRestResponse<AuthResult> response)
    {
        if (response == null || response.StatusCode != HttpStatusCode.OK
            || response.Data == null || string.IsNullOrEmpty(response.Data.access_token))
        {
           // OnAuthenticationFailed();
        }
        else
        {               
        }
    }

感谢任何帮助。

【问题讨论】:

    标签: windows-phone-8 google-oauth access-token refresh-token


    【解决方案1】:

    需要设置内容类型

    request.ContentType = "application/x-www-form-urlencoded";
    

    这是我的 .net 示例,不确定它是否适用于 windows-phone,但它可能会有所帮助

     class TokenResponse
        {
            public string access_token { get; set; }
            public string token_type { get; set; }
            public string expires_in { get; set; }
            public string refresh_token { get; set; }
        }
    
    /// <summary>
    
            /// exchanges the authetncation code for the refreshtoken and access token
            /// </summary>
            /// <param name="code"></param>
            /// <returns></returns>
            public static string exchangeCode(string code)
            {
    
    
                string url = "https://accounts.google.com/o/oauth2/token";
                string postData = string.Format("code={0}&client_id={1}&client_secret={2}&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code", code, Properties.Resources.clientId, Properties.Resources.secret);
    
    
                // Create a request using a URL that can receive a post. 
                WebRequest request = WebRequest.Create(url);
                // Set the Method property of the request to POST.
    
                request.Method = "POST";
    
                byte[] byteArray = Encoding.UTF8.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, 0, byteArray.Length);
                // Close the Stream object.
                dataStream.Close();
                // Get the response.
                WebResponse response = request.GetResponse();
                // Display the status.
                Console.WriteLine(((HttpWebResponse)response).StatusDescription);
                // Get the stream containing content returned by the server.
                dataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.
                StreamReader reader = new StreamReader(dataStream);
                // Read the content.
                string responseFromServer = reader.ReadToEnd();
    
                TokenResponse tmp = JsonConvert.DeserializeObject<TokenResponse>(responseFromServer);
    
                // Display the content.
                Console.WriteLine(responseFromServer);
                // Clean up the streams.
                reader.Close();
                dataStream.Close();
                response.Close();
                return tmp.refresh_token;
            }
    

    Oauth simple

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-11
      • 2019-03-08
      • 2020-04-14
      • 2018-10-20
      • 2021-04-01
      • 2019-05-31
      • 2023-04-04
      • 2020-01-09
      相关资源
      最近更新 更多