Warning: Due to issues with the Adobe Flash Player, the REST API is essentially unavailable when embedding Flex into Visualforce. This is because it strips out the Auth header and also ignores some of the basic REST HTTP actions. Everything works as expected under AIR.
Also, due to Adobe's lack of official support for Flex - this project will probably not be updated.
You’ll need to import the class:
import com.force.oauth.OAuthConnection;
From there, just use the constructor:
var oauth:OAuthConnection = new OAuthConnection("{PUBLICKEY}","{PRIVATEKEY}","{CALLBACK}”);
After that you can have the connection start the process of creating a web view for the user, and then requesting and storing the necessary tokens. To do this, give the login method the main display stage and an aysncronous responder as a callback:
oauth.login(this.stage,new mx.rpc.AsyncResponder(loginHandler,com.force.utility.util.genericError));The RESTConnection class can utilize either a session ID and server URL from logged in credentials, or, more commonly, work in tandem with OAuthConnection.
To use the session ID and server URL:
private function login():void { var oauthConnection:OAuthConnection = new OAuthConnection("PUBLICKEY","PRIVATEKEY","CALLBACK"); oauthConnection.login(this.stage,new mx.rpc.AsyncResponder(postLogin,com.force.utility.util.genericError)); } public function postLogin(restResult:Object, token:Object):void { if(restResult != null) { var lr:LoginRequest = new LoginRequest(OAuthConnection.SOAPLoginRequest()); lr.callback = new com.salesforce.AsyncResponder( search, com.force.utility.util.genericError ); this.conn.login(lr); } } public function search(result:Object):void { // continue implementation code as normal here }
private function login():void { rest = new RESTConnection(); rest.oauthConnection = new OAuthConnection("{PUBLICKEY}","{PRIVATEKEY}","{CALLBACK}"); rest.oauthConnection.login(this.stage,new mx.rpc.AsyncResponder(loginHandler,com.force.utility.util.genericError)); } private function loginHandler(loginResult:Object, token:Object):void { rest.oauth = loginResult; rest.query("SELECT ID, Name from Contact LIMIT 5",new mx.rpc.AsyncResponder(queryHandler,com.force.utility.util.genericError)); }Uses a slightly different class, as the dynamically generated browser can't use HTMLLoader:
private function login():void { rest = new RESTConnection(); rest.oauthConnection = new OAuthConnectionMobile(“PUBLICKEY","PRIVATEKEY","CALLBACKURL"); rest.oauthConnection.login(this.stage,new mx.rpc.AsyncResponder(loginHandler,com.force.utility.util.genericError)); }Query:
rest.query("SELECT ID, Name from Contact LIMIT 5",new mx.rpc.AsyncResponder(queryHandler,com.force.utility.util.genericError)); }Get Object By Idprivate function queryHandler(queryResult:Array, token:Object):void { trace("1::"+ObjectUtil.toString(queryResult)); }
rest.getObjectById(“Account”,”{ACCOUNTID}”,new mx.rpc.Responder(handleResult,com.force.utility.util.genericError)); private function handleResult(result:Object,token:Object):void { trace(result.Name); }
Creating data
var newContact:Object = new Object(); newContact.FirstName = "Jane"; newContact.LastName = "Jones"; rest.create(newContact,"Contact",new AsyncResponder(com.force.utility.util.genericTrace,com.force.utility.util.genericError));
Updating data
oldContact.FirstName = "Jane"; oldContact.LastName = "Smith"; rest.create(oldContact,"Contact",new AsyncResponder(com.force.utility.util.genericTrace,com.force.utility.util.genericError));
Accessing a File
rest.getFileByURI({URI},new AsyncResponder(com.force.utility.util.genericTrace,com.force.utility.util.genericError));
Deleting data
Since Flex does not natively support the DELETE HTTP method, the library needs to use some third party tools to create a proper delete request for the REST API. To do this, you need to install the following libraries:
- as3httpclientlib @ code.google
- as3corelib @ github
- as3crypto @ code.google
The delete request also works differently to distinct this portion of the code the rest of the library that doesn't require the above libraries. To perform a delete request, first create a DeleteRequest object and pass in your RESTConnection:
var delReq:DeleteRequest = new DeleteRequest(rest);
After that, the DeleteRequest behaves in a similar manner as the rest of the library:
delReq.deleteObject(resultGrid.selectedItem.Id,"Contact",new AsyncResponder(refreshData,com.force.utility.util.genericError));