Created
July 7, 2014 09:31
-
-
Save junojisan/1b3e3e7fedd743bb54d4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing class OAuthCallbackController { | |
public OAuthModel oauthModel {get; set;} | |
public String resBody {get; set;} | |
public List<Account> accounts {get; set;} | |
public OAuthCallbackController(){ | |
String error = ApexPages.currentPage().getParameters().get('error'); | |
String errorDescription = ApexPages.currentPage().getParameters().get('error_description'); | |
if(String.isEmpty(error)){ | |
requestToken(); | |
}else{ | |
ApexPages.addMessage( | |
new ApexPages.Message( | |
ApexPages.Severity.ERROR, 'error:' + error + ' description:' + errorDescription | |
) | |
); | |
} | |
} | |
private void requestToken(){ | |
String reqBody = ''; | |
reqBody += 'code=' + EncodingUtil.urlEncode(ApexPages.currentPage().getParameters().get('code'), 'UTF-8'); | |
reqBody += '&'; | |
reqBody += 'grant_type=' + EncodingUtil.urlEncode('authorization_code', 'UTF-8'); | |
reqBody += '&'; | |
reqBody += 'client_id=' + EncodingUtil.urlEncode(OAuthInitiateController.CLIENT_ID, 'UTF-8'); | |
reqBody += '&'; | |
reqBody += 'client_secret=' + EncodingUtil.urlEncode(OAuthInitiateController.CLIENT_SECRET, 'UTF-8'); | |
reqBody += '&'; | |
reqBody += 'redirect_uri=' + EncodingUtil.urlEncode(OAuthInitiateController.CALLBACK_URL, 'UTF-8'); | |
// Setup->Security->Remote site settings. endpoint = https://login.salesforce.com/services/oauth2/token の設定もしておくこと | |
String authorizeUrl = 'https://login.salesforce.com/services/oauth2/token'; | |
Http http = new Http(); | |
HttpRequest req = new HttpRequest(); | |
req.setEndpoint(authorizeUrl); | |
req.setMethod('POST'); | |
req.setBody(reqBody); | |
HttpResponse res = http.send(req); | |
resBody = res.getBody(); | |
JSONParser parser = JSON.createParser(resBody); | |
oauthModel = (OAuthModel)parser.readValueAs(OAuthCallbackController.OAuthModel.class); | |
ApexPages.addMessage( | |
new ApexPages.Message( | |
ApexPages.Severity.INFO, 'Http Status:' + res.getStatusCode() + ' ' + res.getStatus() | |
) | |
); | |
} | |
public PageReference getAccounts(){ | |
String restApiUrl = | |
oauthModel.instance_url + | |
'/services/data/v30.0/query/?q=' + | |
EncodingUtil.urlEncode('Select Id, Name From Account Limit 10', 'UTF-8'); | |
Http http = new Http(); | |
HttpRequest req = new HttpRequest(); | |
req.setHeader('Authorization', 'Bearer ' + oauthModel.access_token); | |
req.setEndpoint(restApiUrl); | |
req.setMethod('GET'); | |
HttpResponse res = http.send(req); | |
if(res.getStatusCode() == 401){ | |
refreshToken(); | |
} | |
JSONParser parser = JSON.createParser(res.getBody()); | |
accounts = new List<Account>(); | |
while(parser.nextToken() != null){ | |
if(parser.getCurrentToken() == JSONToken.START_ARRAY){ | |
while(parser.nextToken() != null){ | |
if(parser.getCurrentToken() == JSONToken.START_OBJECT){ | |
Account acc = (Account)parser.readValueAs(Account.class); | |
accounts.add(acc); | |
} | |
} | |
} | |
} | |
return null; | |
} | |
public void refreshToken(){ | |
// Refresh Tokenを使ってAccess Tokenを取得する | |
// Access Tokenが取得できたら取引先を再取得する | |
// ※サンプルでは割愛 | |
} | |
public class OAuthModel { | |
public String id {get; set;} | |
public String issued_at {get; set;} | |
public String refresh_token {get; set;} | |
public String instance_url {get; set;} | |
public String signature {get; set;} | |
public String access_token {get; set;} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment