Created
August 4, 2014 06:43
-
-
Save junojisan/67d94fa8d9c1fc57c87f 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
private void requestToken(){ | |
// アクセストークンを取得するためのリクエストボディを組立て | |
String reqBody = ''; | |
reqBody += 'code=' + EncodingUtil.urlEncode(ApexPages.currentPage().getParameters().get('code'), 'UTF-8'); | |
reqBody += '&grant_type=' + EncodingUtil.urlEncode('authorization_code', 'UTF-8'); | |
reqBody += '&client_id=' + EncodingUtil.urlEncode(OAuthInitiateController.CLIENT_ID, 'UTF-8'); | |
reqBody += '&client_secret=' + EncodingUtil.urlEncode(OAuthInitiateController.CLIENT_SECRET, 'UTF-8'); | |
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'; | |
// Calloutでアクセストークンを要求 | |
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(); | |
// Salesforceからのレスポンス(JSON)を解析し、OAuthModelに設定 | |
JSONParser parser = JSON.createParser(resBody); | |
oauthModel = (OAuthModel)parser.readValueAs(OAuthCallbackController.OAuthModel.class); | |
// Salesforceからのレスポンスをデバッグ用にメッセージに設定 | |
ApexPages.addMessage( | |
new ApexPages.Message( | |
ApexPages.Severity.INFO, 'Http Status:' + res.getStatusCode() + ' ' + res.getStatus() | |
) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment