2010年1月23日 星期六

OAuth to acess JaiKu in 2 min

Step 1. You have to have a JaiKu account. Then, get you customer "key" and "secret"

Here is where you can get your key and secret. (http://www.jaiku.com/api/keys)

Step 2. Use "OAuth Test Client" for following steps.

A. Use your customer "Key" and "Secret" with endpoint "http://www.jaiku.com/api/request_token" to and click on "reqest_token" button to get your Request Token as follows;

oauth_token_secret=efc0a966fcb64fdf80b639b3f814a640&oauth_token=f84d993f9046417aa7370e48b18a4f50

B. You can't use this token now, because it is not Authorized by "JaiKu.com" yet.

So, copy oauth_token and oauth_token_secret to "OAuth Test Client" again and change the endporint to "http://www.jaiku.com/api/authorize" and press "authorize" button.

You will see a web page asking you to press "Authorize" this token.
If you agree with that. This token is authorized. But you have to use this authorized token to get the Access Token.

C. Use "OAuth Test Client" to get your Access Token again. Change your endpoint to "http://www.jaiku.com/api/access_token" and click on "access token" button.

or you can see your Activated API Tokens in (http://www.jaiku.com/api/tokens)

Step 3. What API and URL do you want to access JaiKu.com? and prepare a Python code to access it.

Python Code: (test.py)

1 import urllib2
2 from oauth import oauth
3 consumer = oauth.OAuthConsumer('79f64f084f9043a9a1a2a5a026c71e77', '09b14b79f8bc46e39da369892a68c6a9')
4 access_token = oauth.OAuthToken('6ef9aeacf88c4d5296e340b09a708521', '1ae85c5bcdcc4827aa3f37902feb7f2a')
5 parameters = {'nick': 'mingderwang', 'method': 'actor_get'}
6 request = oauth.OAuthRequest.from_consumer_and_token(
7 oauth_consumer=consumer,
8 token=access_token,
9 http_url='http://api.jaiku.com/json',
10 http_method='POST',
11 parameters=parameters)
12 request.sign_request(
13 oauth.OAuthSignatureMethod_HMAC_SHA1(),
14 consumer,
15 access_token)
16 result = urllib2.urlopen(urllib2.Request(
17 'http://api.jaiku.com/json',
18 request.to_postdata()))
19 print result.read()
20 result.close()

Step 4. Python test.py to get result as follows;


{"status": "ok", "rv": {"actor": {"avatar_updated_at": "2009-10-28 08:37:49.560840", "privacy": 3, "extra": {"family_name": "Wang", "icon": "mingderwang@jaiku.com/avatar_96e850b8f9dc4814bab9130a9d4a0d4a", "contact_count": 1, "given_name": "Ming-der"}, "nick": "mingderwang@jaiku.com", "deleted_at": null, "type": "user"}}, "servertime": "2010-01-23 10:46:01.523307"}


Where, you want to get JSON format data by using actor_get API with a nick name called "mingderwang" (my account).

use POST http command and OAuthSignatureMethod is HMAC_SHA1


Step 5. If you try it again for other API, make sure your access tokens are still ACTIVATED.


Good luck!

If you still have any question (email me ming@katdc.com)




沒有留言: