Jump to content
  • 0

Authenticating through mobile app


MLaya

Question

Hellow everyone, 

I have implemented a sign in form an button in html:

<ons-button onclick="login()">Sign in</ons-button>

The following Javascript is as follows:


var token_ 
var userName = "clientID"; 
var passWord = "secretKey"; // app clientSecret
var caspioTokenUrl = "https://xxx123.caspio.com/oauth/token"; 
var request = new XMLHttpRequest(); 

function getToken(url, clientID, clientSecret) {
    var key;           
    request.open("POST", url, true); 
    request.setRequestHeader("Content-type", "application/json");
    request.send("grant_type=client_credentials&client_id="+clientID+"&"+"client_secret="+clientSecret); // specify the credentials to receive the token on request
    request.onreadystatechange = function () {
        if (request.readyState == request.DONE) {
            var response = request.responseText;
            var obj = JSON.parse(response); 
            key = obj.access_token; //store the value of the accesstoken
            token_ = key; 
            }
    }
}

getToken(caspioTokenUrl, userName, passWord);

var login = function CallWebAPI() {
    var params {"ClientID" : "username", "ClientSecret" : "password"};
    var request_ = new XMLHttpRequest(params);        
    request_.open("GET", "https://xxx123.caspio.com/rest/v1/tables", true);
    request_.setRequestHeader("Authorization", "Bearer "+ token_);
    request_.send();
    request_.onreadystatechange = function () {
        if (request_.readyState == 4 && request_.readyState == 200) {
            var response = request_.responseText;
            var obj = JSON.parse(response); 
           
        }
    }
} 

It still does not work for me and I'm not quite sure what I am doing wrong.

Any help is very much appreciated!  Thank you all!

Link to comment
Share on other sites

1 answer to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...