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
@Override | |
public void onClick(View v) { | |
// ユーザIDの取得 | |
EditText etUsername = (EditText)findViewById(R.id.et_username); | |
String username = etUsername.getText().toString(); | |
// パスワードの取得 | |
EditText etPassword = (EditText)findViewById(R.id.et_password); | |
String password = etPassword.getText().toString(); | |
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
@Override | |
public PartnerConnection loadInBackground() { | |
PartnerConnection connection = null; | |
ConnectorConfig config = new ConnectorConfig(); | |
config.setUsername(username); | |
config.setPassword(password); | |
try { | |
// PartnerConnectionのインスタンス生成時にログインが実行される |
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
@Override | |
public void onLoadFinished(Loader<PartnerConnection> loader, PartnerConnection connection) { | |
this.connection = connection; | |
if(connection == null){ | |
Toast.makeText(this, "ログインに失敗しました。", Toast.LENGTH_SHORT).show(); | |
}else{ | |
Toast.makeText(this, "ログインに成功しました。", Toast.LENGTH_SHORT).show(); | |
} | |
} |
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="OAuthInitiateController"> | |
<apex:form > | |
<apex:pageBlock title="SalesforceだけでOAuthを試してみる"> | |
<apex:commandButton action="{!initiate}" value="OAuth認証フロー開始" /> | |
</apex:pageBlock> | |
</apex:form> | |
</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
public with sharing class OAuthInitiateController { | |
public static final String CLIENT_ID = <CLIENT_ID>; | |
public static final String CLIENT_SECRET = <CLIENT_SECRET>; | |
public static final String CALLBACK_URL = 'https://xxxx/apex/OAuthCallback'; | |
public PageReference initiate(){ | |
String authorizeUrl = 'https://login.salesforce.com/services/oauth2/authorize'; | |
authorizeUrl += '?response_type=code'; | |
authorizeUrl += '&client_id=' + CLIENT_ID; |
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:form id="fm"> | |
<apex:outputPanel rendered="{!NOT(ISNULL(resBody))}"> |
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'); |
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'; | |
} | |
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 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 |
OlderNewer