2

我正在尝试在 Tizen 中使用 oAuth 2.0 实现 google 身份验证。我正在按照这里的步骤进行操作。根据链接中的说明,我可以获得用户代码。但我总是收到获取访问和刷新令牌的无效请求。我的要求如下。

var urlToken ="https://accounts.google.com/o/oauth2/token?"+ 
        encodeURI("client_id=<<my client id>>&" +
        "client_secret=<<my client secret>>&" + 
        "code=<<Device code received in first step>>&" +
        "grant_type=authorization_code");
$.ajax({
    url:urlToken,
    type:"POST",        
    headers:{
        "Content-Type": "application/x-www-form-urlencoded",
        "Content-length" : "250"
    },
    accepts: "applicatin/json",
    success:function(response){
        console.log("access token response success");
        console.log(response.access_token)
    },
    error:failure
});

我无法弄清楚出了什么问题。请更新它,还有其他方法可以实现它。

注意:我正在尝试从 Tizen Webapp 实现这一点。

4

1 回答 1

1

我得到了使用以下代码的东西。我在查询字符串中标记数据以及明确设置内容类型和内容长度时犯了一个错误。内容类型默认为“application/x-www-form-urlencode”。通过随机点击得到解决方案。

var urlToken ="https://accounts.google.com/o/oauth2/token"+ 
var dataValue = "client_id=<<my client id>>&" +
        "client_secret=<<my client secret>>&" + 
        "code=<<Device code received in first step>>&" +
        "grant_type=http://oauth.net/grant_type/device/1.0";
$.ajax({
    url:urlToken,
    data:dataValue,
    crossDomain:true,
    type:"POST",
    success:function(response){
      if(response.error != null){
            <<Call the same function again>>;
        }
        else{
            console.log("Access Token :" + response.access_token);
            console.log("Token Type : " + response.token_type);
            console.log("Expires : " + response.expires_in);
            console.log("Refresh Token : " + response.refresh_token);
        }
    },
    error:failure
});

谢谢WTK

我相信有所帮助

于 2013-03-20T13:53:46.397 回答