0

I make a request for a oAuth request token and I do that by visiting a special URL. The result of this request (content of page) is this, how can I get the auth_token form this?

oauth_token=TBFdNoytaizrfMAWNZ6feqssNz3BsozHk5AesIioX8u8Ec&oauth_token_secret=DtQ3jiUIe33VdRcBAKwVQJRWpgKtEHi3m1yl3k0nlsHCBj0&oauth_callback_confirmed=true

4 Answers 4

1

You could find it like this:

var string = 'oauth_token=TBFdNoytaizrfMAWNZ6feqssNz3BsozHk5AesIioX8u8Ec&oauth_token_secret=DtQ3jiUIe33VdRcBAKwVQJRWpgKtEHi3m1yl3k0nlsHCBj0&oauth_callback_confirmed=true';

var match = /oauth_token=([^&]*)/.exec(string);
if(match)
{
    var oauth_token = match[1];
}

Also I don't think it's a good idea to show your oauth_token_secret here as I think that only you should know that secret, right?

Sign up to request clarification or add additional context in comments.

Comments

1
var oAuth = /auth_token=(.*?)&/.exec( str )[1];

Comments

1
 // token param + value
 var tokenParam = ostr.split("&")[0];

 // only token value
 var tokenValue = tokenParam.split("=")[1];

Comments

1

The result of the request is in the same format as a query string. This can be parsed using common JS functions.

var str = "oauth_token=TBFdNoytaizrfMAWNZ6feqssNz3BsozHk5AesIioX8u8Ec&oauth_token_secret=DtQ3jiUIe33VdRcBAKwVQJRWpgKtEHi3m1yl3k0nlsHCBj0&oauth_callback_confirmed=true";

var auth_token = getQueryVariable("oauth_token", str);
console.log(auth_token);

function getQueryVariable(variable, query) {
    if (!query) query = window.location.search.substring(1);
    var vars = query.split('&');
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split('=');
        if (decodeURIComponent(pair[0]) == variable) {
            return decodeURIComponent(pair[1]);
        }
    }
    console.log('Query variable %s not found', variable);
}​

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.