Search code examples
javascriptjqueryoauth-2.0tizentizen-web-app

How to implement oAuth 2.0 in Tizen


I'm trying to implement google authentication using oAuth 2.0 in Tizen. I'm following the step from here. Based on the instructions from the link, I can able to obtain the user code. but i'm always getting invalid request for obtaining the access and refresh token. My request is as follows.

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
});

I couldn't able to figure out what goes wrong. Please update it there is any other way to implement the same.

Note: I'm trying to implement this from the Tizen Webapp.


Solution

  • I got things working with the following code. I made a mistake by marking the data in query string as well as setting content-type and content-length explicitly. Content-type is the by default "application/x-www-form-urlencode". Got the solution by random click.

    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
    });
    

    Thanks WTK

    I believe this and this will be helpful