简单的skydrive sdk for python
我大微软竟然没有提供skydrive sdk的python版本,对于skydrive这么好的东西我可是舍不得,最近在捣鼓python,就顺手写了个
import urllib,json AuthorizeServer='https://login.live.com/oauth20_authorize.srf' OauthServer='https://login.live.com/oauth20_token.srf' ClientId=''#CLIENT_ID ClientSecret=''#CLIENT_SECRET Scope='wl.offline_access wl.skydrive_update' RedirectUri='http://live.eslizn.com/callback' ApiServer='https://apis.live.net/v5.0/' Token=None def SaveRefreshToken(refresh): return ''
def ReadRefreshToken(): return '' def Get(url): return urllib.urlopen(url).read() def Post(url,data): return urllib.urlopen(url,urllib.urlencode(data)).read() def GetTokenByCode(code): data={} data['client_id']=ClientId data['redirect_uri']=RedirectUri data['client_secret']=ClientSecret data['code']=code data['grant_type']='authorization_code' data=Post(OauthServer,data) var=json.loads(data) if var.get('access_token') and var.get('refresh_token'): SaveRefreshToken(var['refresh_token']) global Token Token=var['access_token'] return var['access_token'] return False def GetTokenByRefresh(refresh): data={} data['client_id']=ClientId data['client_secret']=ClientSecret data['redirect_uri']=RedirectUri data['refresh_token']=refresh data['grant_type']='refresh_token' data=Post(OauthServer,data) var=json.loads(data) if var.get('access_token') and var.get('refresh_token'): SaveRefreshToken(var['refresh_token']) global Token Token=var['access_token'] return var['access_token'] return False def GetToken(): global Token GetTokenByRefresh(ReadRefreshToken()) if Token: return Token return False def Request(path): global Token if GetToken(): url=ApiServer url+=path url+='?access_token='+str(Token) return json.loads(Get(url)) return False def AuthorizeUri(): url=AuthorizeServer+'?' url+='client_id='+ClientId+'&' url+='scope='+urllib.quote(Scope)+'&' url+='response_type=code&' url+='redirect_uri='+urllib.quote(RedirectUri) return url