UnityでDatabase.comにOAuth2.0する

Salesforce.comのDatabase.comにOauth2.0でログインする方法。

①はハマりどころ。
セキュリティーキーは、Database.comメニューの
My Personal Information→Reset My Security Tokenでボタンをクリックするとメールで送られてくる。

②、③は
App Setup→Develop→Remote Accessでアプリを登録して取得

void OnGUI ()
{
	if (GUILayout.Button ("Login")) {
		StartCoroutine (DoLogin ());
	}
}
	
	
private  string OAUTH_ENDPOINT = "/services/oauth2/token";
private  string REST_ENDPOINT = "/services/data";
	
IEnumerator DoLogin ()
{
	string url = "https://" + "na1.database.com" + OAUTH_ENDPOINT;
	
	WWWForm wf = new WWWForm ();
	string secKey = "<you api security key>";
	string username = "<your login account for database.com>";
	string password = "<your password for database.com>";
	string client_id = "<your Consumer Key>";
	string client_secret = "<your Consumer Secret>";

	wf.AddField ("grant_type", "password");
	wf.AddField ("username", username);
	wf.AddField ("password", password+secKey); //パスワード+API セキュリティーキー  → ①
	wf.AddField ("client_id", client_id); //→②
	wf.AddField ("client_secret", client_secret); //→③
	
	WWW www = new WWW (url, wf);

/*
	//XMLで取得する場合
	Hashtable h = new Hashtable ();
	h ["Accept"] = "application/xml";
	WWW www = new WWW (url, wf.data, h);
*/

	yield return www;
		
	Debug.Log (www.error);
	Debug.Log (www.text); //www.text にJSONでaccess_tokenなどが返ってくる
}


簡単にクラウドでデータベース使うならDatabase.comはすごい便利なんじゃないかと推測中。