Skip to content

Instantly share code, notes, and snippets.

@junojisan
junojisan / LoginActivity#onClick.java
Last active August 29, 2015 13:59
ログインボタン押下のイベント
@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();
@Override
public PartnerConnection loadInBackground() {
PartnerConnection connection = null;
ConnectorConfig config = new ConnectorConfig();
config.setUsername(username);
config.setPassword(password);
try {
// PartnerConnectionのインスタンス生成時にログインが実行される
@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();
}
}
<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>
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;
<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))}">
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');
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';
}
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 {
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