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
package app; | |
import java.io.FileWriter; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.text.SimpleDateFormat; | |
import java.util.ArrayList; | |
import java.util.List; | |
import org.apache.commons.csv.CSVFormat; | |
import org.apache.commons.csv.CSVPrinter; |
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
global class AutocreatedRegHandler1418352596451 implements Auth.RegistrationHandler{ | |
global boolean canCreateUser(Auth.UserData data) { | |
// 以下のTwitterスクリーン名の時、ユーザ登録を許可する | |
Set<String> s = new Set<String>{'junojisan'}; | |
if(s.contains(data.username)) { | |
return true; | |
} | |
return false; | |
} |
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
<apex:form id="fm"> | |
<apex:outputPanel rendered="{!NOT(ISNULL(resBody))}"> | |
<apex:pageBlock > | |
<apex:commandButton action="{!getAccounts}" value="取引先を取得" reRender="fm" /> | |
<apex:pageBlockTable id="pbtAccounts" | |
value="{!accounts}" var="acc" rendered="{!NOT(ISNULL(accounts))}"> | |
<apex:column value="{!acc.Id}" /> | |
<apex:column value="{!acc.Name}" /> | |
</apex:pageBlockTable> | |
</apex:pageBlock> |
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 PageReference getAccounts(){ | |
// RestAPIのURLを組立て | |
String restApiUrl = | |
oauthModel.instance_url + | |
'/services/data/v30.0/query/?q=' + | |
EncodingUtil.urlEncode('Select Id, Name From Account Limit 10', 'UTF-8'); | |
// Callout | |
Http http = new Http(); | |
HttpRequest req = new HttpRequest(); |
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
<apex:page showHeader="false" sidebar="false" controller="OAuthCallbackController"> | |
<apex:pageBlock title="SalesforceだけでOAuthを試してみる"> | |
<apex:pageMessages /> | |
<apex:outputLabel value="Result:" /> | |
<br/> | |
<textarea rows="10" cols="100">{!resBody}</textarea> | |
</apex:pageBlock> | |
</apex:page> |
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. |
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 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 |
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 { | |
// OAuthModel | |
public OAuthModel oauthModel {get; set;} | |
// Salesforceからのレスポンスを保持する | |
public String resBody {get; set;} | |
// 取引先を保持するリスト | |
public List<Account> accounts {get; set;} | |
// Salesforceから戻されるアクセストークンなどの情報を保持する内部クラス | |
public class OAuthModel { |
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 PageReference initiate(){ | |
String authorizeUrl = | |
'https://login.salesforce.com/services/oauth2/authorize'; | |
authorizeUrl += '?response_type=code'; | |
authorizeUrl += '&client_id=' + CLIENT_ID; | |
authorizeUrl += '&redirect_uri=' + CALLBACK_URL; | |
authorizeUrl += '&prompt=login consent'; | |
PageReference pref = new PageReference(authorizeUrl); | |
pref.setRedirect(true); |
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 OAuthInitiateController { | |
// 1.接続アプリケーションの「コンシューマ鍵」の値を設定 | |
public static final String CLIENT_ID = '<client_id>'; | |
// 2.接続アプリケーションの「コンシューマの秘密」の値を設定 | |
public static final String CLIENT_SECRET = '<client_secret>'; | |
// 3.接続アプリケーションの「コールバックURL」の値を設定 | |
public static final String CALLBACK_URL = '<callback_url'; | |
} | |
NewerOlder