Last active
August 29, 2015 14:14
-
-
Save nutti/44a1a96c1f39f29b54d6 to your computer and use it in GitHub Desktop.
[PhoneGap/Cordova] InAppBrowserを用いてDatabase.comの認証をする方法 ref: http://qiita.com/nutti/items/a154b6ef0c0608bd99ec
This file contains hidden or 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
$ cordova platform rm ios |
This file contains hidden or 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
<feature name="InAppBrowser"> | |
<param name="ios-package" value="CDVInAppBrowser" /> | |
</feature> |
This file contains hidden or 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
$ cordova plugin add org.apache.cordova.inappbrowser |
This file contains hidden or 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
$ cordova run ios |
This file contains hidden or 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
<script type="text/javascript" src="js/salesforceWrapper.js"></script> |
This file contains hidden or 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
var sfw; | |
sfw = new SalesforceWrapper(); // SalesforceWrapperの初期化 | |
sfw.login( successCallback ); // Salesforceへログイン | |
// ログインが成功したときに呼び出される関数 | |
function successCallback() | |
{ | |
// ログインが成功したときの処理など | |
} |
This file contains hidden or 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
function SalesforceWrapper() | |
{ | |
// 認証パラメータ | |
this.loginUrl = 'https://login.salesforce.com/'; | |
this.clientId = 'YOUR_KEY_HERE'; // Database.comで取得したAPI key | |
this.redirectUri = 'https://login.salesforce.com/services/oauth2/success'; | |
// クラス変数 | |
this.cb = undefined; // InAppBrowser | |
this.client = undefined; // forceTk | |
this.init(); | |
} | |
SalesforceWrapper.prototype.init = function() { | |
this.client = new forcetk.Client(this.clientId, this.loginUrl); | |
}; | |
SalesforceWrapper.prototype.login = function (successCallback) { | |
// 認証画面の表示 | |
var url = this.getAuthorizeURL(this.loginUrl, this.clientId, this.redirectUri); | |
this.cb = window.open(url, "_blank", 'location=no,toolbar=no,clearsessioncache=yes'); | |
this.loginSuccess = successCallback; | |
// 画面遷移した場合に呼び出される関数の登録 | |
var self = this; | |
self.cb.addEventListener('loadstop', function(e) { | |
var loc = e.url; | |
// 認証が通った場合の処理 | |
if (loc.indexOf(self.redirectUri) != -1){ | |
self.cb.close(); // 認証画面を閉じる | |
self.sessionCallback(unescape(loc)); | |
} | |
}); | |
}; | |
// 認証用のURLを取得 | |
SalesforceWrapper.prototype.getAuthorizeURL = function (loginUrl, clientId, redirectUri) { | |
return loginUrl + 'services/oauth2/authorize?' + '&response_type=token&client_id=' + encodeURIComponent(clientId) + '&redirect_uri=' + encodeURIComponent(redirectUri); | |
}; | |
// 認証が通り認証画面が閉じた後に実行される関数 | |
SalesforceWrapper.prototype.sessionCallback = function (loc) { | |
var oauthResponse = {}; | |
var fragment = loc.split("#")[1]; | |
if (fragment) { | |
var nvps = fragment.split('&'); | |
for (var nvp in nvps) { | |
var parts = nvps[nvp].split('='); | |
oauthResponse[parts[0]] = unescape(parts[1]); | |
} | |
} | |
if (typeof oauthResponse === 'undefined' || typeof oauthResponse['access_token'] === 'undefined') { | |
console.log("error"); | |
} | |
else { | |
this.client.setSessionToken(oauthResponse.access_token, null, oauthResponse.instance_url); | |
if (this.loginSuccess){ | |
this.loginSuccess(); | |
} | |
} | |
this.loginSuccess = undefined; | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment