我有一个Windows Phone 8应用,正在尝试执行Google身份验证。 我进入登录页面,登录后将我带到同意页面。 单击“允许访问”后,我没有得到访问令牌并刷新令牌作为响应。
我得到的答复如下:
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
{
}
}
StatusCode是错误请求。
这是我的代码:
failOnDuplicatePaymentMethod
感谢您的帮助。
答案 0 :(得分:0)
需要设置内容类型
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;
}