目次

2008年11月8日土曜日

Orb APIを使ってみた~ログイン(session.login)編〜

今日一日、OrbのAPIと格闘した私です。初心者にはきついんですよ。
全部英語だし!んぎゃぎゃ。
で苦労した結果のまとめ&How To でございます。

1,始めに
まずOrbAPIを使うには、ほとんどの場合authenticateが必要なんだそうな。それを行うための“session.login”なんであります。

2,まずはサンプルを試してみる。
https://api.orb.com/orb/xml/session.login?apiKey=※ここにはOrbから発行してもらったAPIKeyを入れます&login=ログイン名&password=パスワード

このURLを入れてみます。すると


<orb version="1.0" action="session.login" login="yasuko_yaka">
<status code="0"/>
−<response>
−<orbSession>
<orbSessionId>01234*****</orbSessionId>
<maxInactiveInterval>120</maxInactiveInterval>
<orbVersion>2.01.0015</orbVersion>
</orbSession>
</response>
</orb>


こんな結果が出力されます。01234*****が私のsessionIdになります!

3,自分でも作ってみましょう。
・サンプルソース(html)
・ajaxProxy.php
・prototype.js
この3つを用意して、同じ階層に置いてあげます。
ajaxProxy.php

<?php
header("Content-Type: text/xml");
if ( substr($_GET['url'], 0, 7) == 'http://' ) {
$handle = fopen($_GET['url'], "rb");
while ( !feof($handle) ) {
echo fread($handle, 8192);
}
fclose($handle);
}
?>

Orbにあったサンプルを利用しました。


prototype.js
こちらから、ソースコードがあるのでコピペ

htmlファイル

<html>
<head>
<!-- We use the Prototype library. Get it at: http://prototypejs.org -->
<script src="prototype.js" type="text/javascript"></script>
</head>
<body>
<script language="javascript">
// End point url of the Orb API.エンドポイントURIとは、ローカル又はリモートの資源及びサービスを特定するためのアドレスです
var orbApiUrl = 'http://api.orb.com/orb/';
// API key. If you want to run this sample, paste your API key below.
var apiKey = '以前習得したOrbAPIkeyを入れます';
// To go around Javascript's cross domain restriction, we use a local proxy.ajaxProxy.phpファイルソースはOrbのサイトに掲載されてるものを利用
var proxyUrl = 'ajaxProxy.php';

/*
* Authenticate
*/
function authenticate(login, password) {
// We make an ajax request to the login method, through the local proxy.
var url = orbApiUrl + 'xml/session.login?apiKey=' + apiKey +
'&l=' + encodeURIComponent(login) +
'&password=' + encodeURIComponent(password);
new Ajax.Request(proxyUrl + '?url=' + escape(url), {
method: 'get',
onSuccess: authenticateSuccessCallback,
onFailure: authenticateFailureCallback
});
};

/*
* authenticateSuccessCallback
* Called when the login ajax request was successful.
*/
function authenticateSuccessCallback(transport) {
// Access the XML dom object within the response.
var dom = transport.responseXML.documentElement;
// Check that the Orb response is OK.
if (checkOrbResponseStatus(dom)) {
// Retrieve the session id from the XML dom object.
sessionId = dom.getElementsByTagName('orbSessionId')[0].firstChild.nodeValue;
// That's it! We have the session id, we can now make call to methods that require authentication.
alert('User is logged in! The session id is: ' + sessionId);
}
};

/*
* authenticateFailureCallback
* Called when the login ajax request failed.
* (this is different from a failure on the Orb side, like if the login/password are incorrect for instance).
*/
function authenticateFailureCallback(transport) {
alert('Error contacting Orb server (' + transport.status + '): ' + transport.responseText);
};

/*
* checkOrbResponseStatus
* This is a generic function that looks into the XML response sent by Orb to see if the status code is 0 (OK).
*/
function checkOrbResponseStatus(dom) {
var error = '';
try {
// Get the XML 'status' node.
var statusNode = dom.getElementsByTagName('status')[0];
// Get the XML 'status' node's 'code' attribute.
var statusCode = statusNode.attributes.getNamedItem('code').value;
if (statusCode != '0') {
// Code different from 0 means there was a problem.
var statusDesc = statusNode.attributes.getNamedItem('desc').value;
// Get the description corresponding to the error.
// WARNING: the description is just for development purposes.
// The text it contains is not intended to be displayed to the end-user.
// You should provide your own text/error messages depending on the error codes.
error = 'Error (' + statusCode + ') : ' + statusDesc;
}
} catch (e) {
error = 'Unknown error.';
}
if (error !== '') {
alert(error);
}
// Return true (boolean) if there was no error.
return (error === '');
}

// Authenticate the user!
authenticate('ログイン名', 'パスワード');
</script>
</body>
</html>


できたら、上のhtmlにアクセス。
成功するとsessionIDが書かれたポップアップが出てきます♪

0 件のコメント: