python 으로 작성된 pocket api wrapper 에서, "pocketpy\auth.py" 를 보면 어떻게 protocol 이 되어 있는지 알 수 있다.
대략적인 순서는 아래와 같다.
- request_token 가져오기 : consumer_key 를 가지고 https://getpocket.com/v3/oauth/request 에 접속해서 code(request token) 를 가져오고,
- access 허락 : https://getpocket.com/auth/authorize?request_token=%s&redirect_uri=%s 에 접속해서 나의 app 이 나의 pocket 계정에 access 하는 것을 허락(accept) 한다.
- access token 가져오기 : 그리고 나서, https://getpocket.com/v3/oauth/authorize 를 통해서 access token 을 얻어올 수 있다.
- Pocket API 사용하기 : 얻어온 request_token 과 access_token 을 이용해서 v3 API: Add / v3 API: Modify / v3 API: Retrieve 를 실행할 수 있다.
Sample python code
!/usr/bin/env python # -*- coding: utf-8 -*- ''' Created on 2013. 3. 11. @author: namh ''' import re #---------------------------------------------------------------------------- if __name__ == "__main__": consumer_key = "12124-ed46226471d4dfbfe68db428" redirect_uri = "http://google.com" AUTH_URL = ("https://getpocket.com/auth/authorize?" "request_token=%s&redirect_uri=%s") REDIRECT_URL = "http://www.google.com/" #################################### # import json import httplib2 from urllib import urlencode http = httplib2.Http('.cache') url = 'https://getpocket.com/v3/oauth/request' headers = { "cache-control":"no-cache", "Connection": "keep-alive", "accept-encoding":"gzip, deflate", "Content-type": "application/json; charset=UTF-8", 'X-Accept' : 'application/json', "user-agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.7 \ (KHTML, like Gecko) Chrome/16.0.912.75 Safari/535.7" } #################################### # Step 1. # Obtain a request token data = { "consumer_key" : consumer_key, "redirect_uri" : redirect_uri, } response, content = http.request(url, "POST", headers=headers, body=json.dumps(data), ) code = None if response['status'] == '200': code = json.loads(content)['code'] print code #################################### # Step 2. # Auth print "please go the following website and authenticate:" print AUTH_URL % (code, REDIRECT_URL) raw_input("When you have completed, press enter:") ''' url = "https://getpocket.com/auth/authorize?request_token="\ + code\ + "&redirect_uri="\ + redirect_uri\ response, content = http.request(url, "GET", headers=headers, ) # content would get the login page of Pocket print response ''' #################################### # Step 3. # Get access token AUTH_TOKEN_URL = "https://getpocket.com/v3/oauth/authorize" data = { "consumer_key" : consumer_key, "code" : code, } response, content = http.request(AUTH_TOKEN_URL, "POST", headers=headers, body=json.dumps(data), ) print response print content access_token = json.loads(content)['access_token'] #################################### # Step 4. # Add content to Pocket data = { "url" : "http://www.economist.com/news/business/\ 21573134-americas-proxy-season-will-pit-management-against-owners-never-shareholders", "consumer_key" : consumer_key, "access_token" : access_token, } url = "http://getpocket.com/v3/add" response, content = http.request(url, "POST", headers=headers, body=json.dumps(data), ) print response print content
from : http://dev.opera.com/articles/view/gentle-introduction-to-oauth/ |
댓글 없음:
댓글 쓰기