I am trying to query a field in visual force page using Javascrip and HTTP Requests (Rest API OAUTH 2.0). But I am not sure how I can get the access Token or the Bearer automatically after every time a user logs in. every how long does the Access token get expired? how can I use their password and username to make the first handshake and grab the access token. I am kind of all over place on this. This needs to be in Ajax, and get the results back in JSON I am trying doing this inside salesforce using visual force page. Its not an outside App

I have read this but I dont wanna use any class for this https://developer.salesforce.com/blogs/developer-relations/2015/07/using-apex-to-integrate-salesforce.html

I just wanna know how I can get the access token for different user one they login and go to the apex page without the user give a permission

I dont wanna do APP https://developer.salesforce.com/page/Digging_Deeper_into_OAuth_2.0_on_Force.com

  • What have you tried so far? Can you edit your question to include any code you might have written? Sep 23, 2016 at 17:07
  • @battery.cord I am not even sure if it is possible right now. I used postman and I was able to query all the stuff that I wanted to see with my own Oauth but that is only with my own password and username. But how do I get the access token via javascript for all every users that login , I have read this developer.salesforce.com/blogs/developer-relations/2015/07/…
    – Metawaa
    Sep 23, 2016 at 17:12
  • 2
    It sounds like you're going about this the (very) hard way. I'd suggest taking a look at the Salesforce AJAX Toolklit
    – Derek F
    Sep 23, 2016 at 17:48
  • @DerekF Perfect I was looking for exactly that
    – Metawaa
    Sep 23, 2016 at 17:50
  • @DerekF Is there anything wrong with this 'type: "GET", url: "/services/data/v35.0/query?q="+''+ {!URLENCODE('SELECT Name, Name FROM Contact WHERE Accountid =' + "'001R0000011zC8C'" )}",'
    – Metawaa
    Sep 23, 2016 at 18:00

1 Answer 1


Since you seem to be working from within Salesforce, you can simply use the Salesforce AJAX Toolkit.

The main advantage there is that you don't need to handle authentication at all.

In one of the example pages towards the end of that documentation, there is an example of how to issue a query.

result = sforce.connection.query("Select Name, Id from User");

records = result.getArray("records");
for (var i=0; i< records.length; i++) {
    var record = records[i];
    log(record.Name + " -- " + record.Id);

That said, I'd normally advise creating/using a controller extension to handle this type of thing (especially if you would otherwise only need to issue a query once for the lifetime of a page). It all depends on what exactly you're trying to accomplish.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .