PageRenderTime 52ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/gdata/service.py

http://radioappz.googlecode.com/
Python | 1564 lines | 1504 code | 13 blank | 47 comment | 25 complexity | 93110a26726421630535b6fd6a2236fe MD5 | raw file
  1. #!/usr/bin/python
  2. #
  3. # Copyright (C) 2006,2008 Google Inc.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """GDataService provides CRUD ops. and programmatic login for GData services.
  17. Error: A base exception class for all exceptions in the gdata_client
  18. module.
  19. CaptchaRequired: This exception is thrown when a login attempt results in a
  20. captcha challenge from the ClientLogin service. When this
  21. exception is thrown, the captcha_token and captcha_url are
  22. set to the values provided in the server's response.
  23. BadAuthentication: Raised when a login attempt is made with an incorrect
  24. username or password.
  25. NotAuthenticated: Raised if an operation requiring authentication is called
  26. before a user has authenticated.
  27. NonAuthSubToken: Raised if a method to modify an AuthSub token is used when
  28. the user is either not authenticated or is authenticated
  29. through another authentication mechanism.
  30. NonOAuthToken: Raised if a method to modify an OAuth token is used when the
  31. user is either not authenticated or is authenticated through
  32. another authentication mechanism.
  33. RequestError: Raised if a CRUD request returned a non-success code.
  34. UnexpectedReturnType: Raised if the response from the server was not of the
  35. desired type. For example, this would be raised if the
  36. server sent a feed when the client requested an entry.
  37. GDataService: Encapsulates user credentials needed to perform insert, update
  38. and delete operations with the GData API. An instance can
  39. perform user authentication, query, insertion, deletion, and
  40. update.
  41. Query: Eases query URI creation by allowing URI parameters to be set as
  42. dictionary attributes. For example a query with a feed of
  43. '/base/feeds/snippets' and ['bq'] set to 'digital camera' will
  44. produce '/base/feeds/snippets?bq=digital+camera' when .ToUri() is
  45. called on it.
  46. """
  47. __author__ = 'api.jscudder (Jeffrey Scudder)'
  48. import re
  49. import urllib
  50. import urlparse
  51. try:
  52. from xml.etree import cElementTree as ElementTree
  53. except ImportError:
  54. try:
  55. import cElementTree as ElementTree
  56. except ImportError:
  57. try:
  58. from xml.etree import ElementTree
  59. except ImportError:
  60. from elementtree import ElementTree
  61. import atom.service
  62. import gdata
  63. import atom
  64. import atom.http_interface
  65. import atom.token_store
  66. import gdata.auth
  67. import gdata.gauth
  68. AUTH_SERVER_HOST = 'https://www.google.com'
  69. # When requesting an AuthSub token, it is often helpful to track the scope
  70. # which is being requested. One way to accomplish this is to add a URL
  71. # parameter to the 'next' URL which contains the requested scope. This
  72. # constant is the default name (AKA key) for the URL parameter.
  73. SCOPE_URL_PARAM_NAME = 'authsub_token_scope'
  74. # When requesting an OAuth access token or authorization of an existing OAuth
  75. # request token, it is often helpful to track the scope(s) which is/are being
  76. # requested. One way to accomplish this is to add a URL parameter to the
  77. # 'callback' URL which contains the requested scope. This constant is the
  78. # default name (AKA key) for the URL parameter.
  79. OAUTH_SCOPE_URL_PARAM_NAME = 'oauth_token_scope'
  80. # Maps the service names used in ClientLogin to scope URLs.
  81. CLIENT_LOGIN_SCOPES = gdata.gauth.AUTH_SCOPES
  82. # Default parameters for GDataService.GetWithRetries method
  83. DEFAULT_NUM_RETRIES = 3
  84. DEFAULT_DELAY = 1
  85. DEFAULT_BACKOFF = 2
  86. def lookup_scopes(service_name):
  87. """Finds the scope URLs for the desired service.
  88. In some cases, an unknown service may be used, and in those cases this
  89. function will return None.
  90. """
  91. if service_name in CLIENT_LOGIN_SCOPES:
  92. return CLIENT_LOGIN_SCOPES[service_name]
  93. return None
  94. # Module level variable specifies which module should be used by GDataService
  95. # objects to make HttpRequests. This setting can be overridden on each
  96. # instance of GDataService.
  97. # This module level variable is deprecated. Reassign the http_client member
  98. # of a GDataService object instead.
  99. http_request_handler = atom.service
  100. class Error(Exception):
  101. pass
  102. class CaptchaRequired(Error):
  103. pass
  104. class BadAuthentication(Error):
  105. pass
  106. class NotAuthenticated(Error):
  107. pass
  108. class NonAuthSubToken(Error):
  109. pass
  110. class NonOAuthToken(Error):
  111. pass
  112. class RequestError(Error):
  113. pass
  114. class UnexpectedReturnType(Error):
  115. pass
  116. class BadAuthenticationServiceURL(Error):
  117. pass
  118. class FetchingOAuthRequestTokenFailed(RequestError):
  119. pass
  120. class TokenUpgradeFailed(RequestError):
  121. pass
  122. class RevokingOAuthTokenFailed(RequestError):
  123. pass
  124. class AuthorizationRequired(Error):
  125. pass
  126. class TokenHadNoScope(Error):
  127. pass
  128. class RanOutOfTries(Error):
  129. pass
  130. class GDataService(atom.service.AtomService):
  131. """Contains elements needed for GData login and CRUD request headers.
  132. Maintains additional headers (tokens for example) needed for the GData
  133. services to allow a user to perform inserts, updates, and deletes.
  134. """
  135. # The hander member is deprecated, use http_client instead.
  136. handler = None
  137. # The auth_token member is deprecated, use the token_store instead.
  138. auth_token = None
  139. # The tokens dict is deprecated in favor of the token_store.
  140. tokens = None
  141. def __init__(self, email=None, password=None, account_type='HOSTED_OR_GOOGLE',
  142. service=None, auth_service_url=None, source=None, server=None,
  143. additional_headers=None, handler=None, tokens=None,
  144. http_client=None, token_store=None):
  145. """Creates an object of type GDataService.
  146. Args:
  147. email: string (optional) The user's email address, used for
  148. authentication.
  149. password: string (optional) The user's password.
  150. account_type: string (optional) The type of account to use. Use
  151. 'GOOGLE' for regular Google accounts or 'HOSTED' for Google
  152. Apps accounts, or 'HOSTED_OR_GOOGLE' to try finding a HOSTED
  153. account first and, if it doesn't exist, try finding a regular
  154. GOOGLE account. Default value: 'HOSTED_OR_GOOGLE'.
  155. service: string (optional) The desired service for which credentials
  156. will be obtained.
  157. auth_service_url: string (optional) User-defined auth token request URL
  158. allows users to explicitly specify where to send auth token requests.
  159. source: string (optional) The name of the user's application.
  160. server: string (optional) The name of the server to which a connection
  161. will be opened. Default value: 'base.google.com'.
  162. additional_headers: dictionary (optional) Any additional headers which
  163. should be included with CRUD operations.
  164. handler: module (optional) This parameter is deprecated and has been
  165. replaced by http_client.
  166. tokens: This parameter is deprecated, calls should be made to
  167. token_store instead.
  168. http_client: An object responsible for making HTTP requests using a
  169. request method. If none is provided, a new instance of
  170. atom.http.ProxiedHttpClient will be used.
  171. token_store: Keeps a collection of authorization tokens which can be
  172. applied to requests for a specific URLs. Critical methods are
  173. find_token based on a URL (atom.url.Url or a string), add_token,
  174. and remove_token.
  175. """
  176. atom.service.AtomService.__init__(self, http_client=http_client,
  177. token_store=token_store)
  178. self.email = email
  179. self.password = password
  180. self.account_type = account_type
  181. self.service = service
  182. self.auth_service_url = auth_service_url
  183. self.server = server
  184. self.additional_headers = additional_headers or {}
  185. self._oauth_input_params = None
  186. self.__SetSource(source)
  187. self.__captcha_token = None
  188. self.__captcha_url = None
  189. self.__gsessionid = None
  190. if http_request_handler.__name__ == 'gdata.urlfetch':
  191. import gdata.alt.appengine
  192. self.http_client = gdata.alt.appengine.AppEngineHttpClient()
  193. def _SetSessionId(self, session_id):
  194. """Used in unit tests to simulate a 302 which sets a gsessionid."""
  195. self.__gsessionid = session_id
  196. # Define properties for GDataService
  197. def _SetAuthSubToken(self, auth_token, scopes=None):
  198. """Deprecated, use SetAuthSubToken instead."""
  199. self.SetAuthSubToken(auth_token, scopes=scopes)
  200. def __SetAuthSubToken(self, auth_token, scopes=None):
  201. """Deprecated, use SetAuthSubToken instead."""
  202. self._SetAuthSubToken(auth_token, scopes=scopes)
  203. def _GetAuthToken(self):
  204. """Returns the auth token used for authenticating requests.
  205. Returns:
  206. string
  207. """
  208. current_scopes = lookup_scopes(self.service)
  209. if current_scopes:
  210. token = self.token_store.find_token(current_scopes[0])
  211. if hasattr(token, 'auth_header'):
  212. return token.auth_header
  213. return None
  214. def _GetCaptchaToken(self):
  215. """Returns a captcha token if the most recent login attempt generated one.
  216. The captcha token is only set if the Programmatic Login attempt failed
  217. because the Google service issued a captcha challenge.
  218. Returns:
  219. string
  220. """
  221. return self.__captcha_token
  222. def __GetCaptchaToken(self):
  223. return self._GetCaptchaToken()
  224. captcha_token = property(__GetCaptchaToken,
  225. doc="""Get the captcha token for a login request.""")
  226. def _GetCaptchaURL(self):
  227. """Returns the URL of the captcha image if a login attempt generated one.
  228. The captcha URL is only set if the Programmatic Login attempt failed
  229. because the Google service issued a captcha challenge.
  230. Returns:
  231. string
  232. """
  233. return self.__captcha_url
  234. def __GetCaptchaURL(self):
  235. return self._GetCaptchaURL()
  236. captcha_url = property(__GetCaptchaURL,
  237. doc="""Get the captcha URL for a login request.""")
  238. def GetGeneratorFromLinkFinder(self, link_finder, func,
  239. num_retries=DEFAULT_NUM_RETRIES,
  240. delay=DEFAULT_DELAY,
  241. backoff=DEFAULT_BACKOFF):
  242. """returns a generator for pagination"""
  243. yield link_finder
  244. next = link_finder.GetNextLink()
  245. while next is not None:
  246. next_feed = func(str(self.GetWithRetries(
  247. next.href, num_retries=num_retries, delay=delay, backoff=backoff)))
  248. yield next_feed
  249. next = next_feed.GetNextLink()
  250. def _GetElementGeneratorFromLinkFinder(self, link_finder, func,
  251. num_retries=DEFAULT_NUM_RETRIES,
  252. delay=DEFAULT_DELAY,
  253. backoff=DEFAULT_BACKOFF):
  254. for element in self.GetGeneratorFromLinkFinder(link_finder, func,
  255. num_retries=num_retries,
  256. delay=delay,
  257. backoff=backoff).entry:
  258. yield element
  259. def GetOAuthInputParameters(self):
  260. return self._oauth_input_params
  261. def SetOAuthInputParameters(self, signature_method, consumer_key,
  262. consumer_secret=None, rsa_key=None,
  263. two_legged_oauth=False, requestor_id=None):
  264. """Sets parameters required for using OAuth authentication mechanism.
  265. NOTE: Though consumer_secret and rsa_key are optional, either of the two
  266. is required depending on the value of the signature_method.
  267. Args:
  268. signature_method: class which provides implementation for strategy class
  269. oauth.oauth.OAuthSignatureMethod. Signature method to be used for
  270. signing each request. Valid implementations are provided as the
  271. constants defined by gdata.auth.OAuthSignatureMethod. Currently
  272. they are gdata.auth.OAuthSignatureMethod.RSA_SHA1 and
  273. gdata.auth.OAuthSignatureMethod.HMAC_SHA1
  274. consumer_key: string Domain identifying third_party web application.
  275. consumer_secret: string (optional) Secret generated during registration.
  276. Required only for HMAC_SHA1 signature method.
  277. rsa_key: string (optional) Private key required for RSA_SHA1 signature
  278. method.
  279. two_legged_oauth: boolean (optional) Enables two-legged OAuth process.
  280. requestor_id: string (optional) User email adress to make requests on
  281. their behalf. This parameter should only be set when two_legged_oauth
  282. is True.
  283. """
  284. self._oauth_input_params = gdata.auth.OAuthInputParams(
  285. signature_method, consumer_key, consumer_secret=consumer_secret,
  286. rsa_key=rsa_key, requestor_id=requestor_id)
  287. if two_legged_oauth:
  288. oauth_token = gdata.auth.OAuthToken(
  289. oauth_input_params=self._oauth_input_params)
  290. self.SetOAuthToken(oauth_token)
  291. def FetchOAuthRequestToken(self, scopes=None, extra_parameters=None,
  292. request_url='%s/accounts/OAuthGetRequestToken' % \
  293. AUTH_SERVER_HOST, oauth_callback=None):
  294. """Fetches and sets the OAuth request token and returns it.
  295. Args:
  296. scopes: string or list of string base URL(s) of the service(s) to be
  297. accessed. If None, then this method tries to determine the
  298. scope(s) from the current service.
  299. extra_parameters: dict (optional) key-value pairs as any additional
  300. parameters to be included in the URL and signature while making a
  301. request for fetching an OAuth request token. All the OAuth parameters
  302. are added by default. But if provided through this argument, any
  303. default parameters will be overwritten. For e.g. a default parameter
  304. oauth_version 1.0 can be overwritten if
  305. extra_parameters = {'oauth_version': '2.0'}
  306. request_url: Request token URL. The default is
  307. 'https://www.google.com/accounts/OAuthGetRequestToken'.
  308. oauth_callback: str (optional) If set, it is assume the client is using
  309. the OAuth v1.0a protocol where the callback url is sent in the
  310. request token step. If the oauth_callback is also set in
  311. extra_params, this value will override that one.
  312. Returns:
  313. The fetched request token as a gdata.auth.OAuthToken object.
  314. Raises:
  315. FetchingOAuthRequestTokenFailed if the server responded to the request
  316. with an error.
  317. """
  318. if scopes is None:
  319. scopes = lookup_scopes(self.service)
  320. if not isinstance(scopes, (list, tuple)):
  321. scopes = [scopes,]
  322. if oauth_callback:
  323. if extra_parameters is not None:
  324. extra_parameters['oauth_callback'] = oauth_callback
  325. else:
  326. extra_parameters = {'oauth_callback': oauth_callback}
  327. request_token_url = gdata.auth.GenerateOAuthRequestTokenUrl(
  328. self._oauth_input_params, scopes,
  329. request_token_url=request_url,
  330. extra_parameters=extra_parameters)
  331. response = self.http_client.request('GET', str(request_token_url))
  332. if response.status == 200:
  333. token = gdata.auth.OAuthToken()
  334. token.set_token_string(response.read())
  335. token.scopes = scopes
  336. token.oauth_input_params = self._oauth_input_params
  337. self.SetOAuthToken(token)
  338. return token
  339. error = {
  340. 'status': response.status,
  341. 'reason': 'Non 200 response on fetch request token',
  342. 'body': response.read()
  343. }
  344. raise FetchingOAuthRequestTokenFailed(error)
  345. def SetOAuthToken(self, oauth_token):
  346. """Attempts to set the current token and add it to the token store.
  347. The oauth_token can be any OAuth token i.e. unauthorized request token,
  348. authorized request token or access token.
  349. This method also attempts to add the token to the token store.
  350. Use this method any time you want the current token to point to the
  351. oauth_token passed. For e.g. call this method with the request token
  352. you receive from FetchOAuthRequestToken.
  353. Args:
  354. request_token: gdata.auth.OAuthToken OAuth request token.
  355. """
  356. if self.auto_set_current_token:
  357. self.current_token = oauth_token
  358. if self.auto_store_tokens:
  359. self.token_store.add_token(oauth_token)
  360. def GenerateOAuthAuthorizationURL(
  361. self, request_token=None, callback_url=None, extra_params=None,
  362. include_scopes_in_callback=False,
  363. scopes_param_prefix=OAUTH_SCOPE_URL_PARAM_NAME,
  364. request_url='%s/accounts/OAuthAuthorizeToken' % AUTH_SERVER_HOST):
  365. """Generates URL at which user will login to authorize the request token.
  366. Args:
  367. request_token: gdata.auth.OAuthToken (optional) OAuth request token.
  368. If not specified, then the current token will be used if it is of
  369. type <gdata.auth.OAuthToken>, else it is found by looking in the
  370. token_store by looking for a token for the current scope.
  371. callback_url: string (optional) The URL user will be sent to after
  372. logging in and granting access.
  373. extra_params: dict (optional) Additional parameters to be sent.
  374. include_scopes_in_callback: Boolean (default=False) if set to True, and
  375. if 'callback_url' is present, the 'callback_url' will be modified to
  376. include the scope(s) from the request token as a URL parameter. The
  377. key for the 'callback' URL's scope parameter will be
  378. OAUTH_SCOPE_URL_PARAM_NAME. The benefit of including the scope URL as
  379. a parameter to the 'callback' URL, is that the page which receives
  380. the OAuth token will be able to tell which URLs the token grants
  381. access to.
  382. scopes_param_prefix: string (default='oauth_token_scope') The URL
  383. parameter key which maps to the list of valid scopes for the token.
  384. This URL parameter will be included in the callback URL along with
  385. the scopes of the token as value if include_scopes_in_callback=True.
  386. request_url: Authorization URL. The default is
  387. 'https://www.google.com/accounts/OAuthAuthorizeToken'.
  388. Returns:
  389. A string URL at which the user is required to login.
  390. Raises:
  391. NonOAuthToken if the user's request token is not an OAuth token or if a
  392. request token was not available.
  393. """
  394. if request_token and not isinstance(request_token, gdata.auth.OAuthToken):
  395. raise NonOAuthToken
  396. if not request_token:
  397. if isinstance(self.current_token, gdata.auth.OAuthToken):
  398. request_token = self.current_token
  399. else:
  400. current_scopes = lookup_scopes(self.service)
  401. if current_scopes:
  402. token = self.token_store.find_token(current_scopes[0])
  403. if isinstance(token, gdata.auth.OAuthToken):
  404. request_token = token
  405. if not request_token:
  406. raise NonOAuthToken
  407. return str(gdata.auth.GenerateOAuthAuthorizationUrl(
  408. request_token,
  409. authorization_url=request_url,
  410. callback_url=callback_url, extra_params=extra_params,
  411. include_scopes_in_callback=include_scopes_in_callback,
  412. scopes_param_prefix=scopes_param_prefix))
  413. def UpgradeToOAuthAccessToken(self, authorized_request_token=None,
  414. request_url='%s/accounts/OAuthGetAccessToken' \
  415. % AUTH_SERVER_HOST, oauth_version='1.0',
  416. oauth_verifier=None):
  417. """Upgrades the authorized request token to an access token and returns it
  418. Args:
  419. authorized_request_token: gdata.auth.OAuthToken (optional) OAuth request
  420. token. If not specified, then the current token will be used if it is
  421. of type <gdata.auth.OAuthToken>, else it is found by looking in the
  422. token_store by looking for a token for the current scope.
  423. request_url: Access token URL. The default is
  424. 'https://www.google.com/accounts/OAuthGetAccessToken'.
  425. oauth_version: str (default='1.0') oauth_version parameter. All other
  426. 'oauth_' parameters are added by default. This parameter too, is
  427. added by default but here you can override it's value.
  428. oauth_verifier: str (optional) If present, it is assumed that the client
  429. will use the OAuth v1.0a protocol which includes passing the
  430. oauth_verifier (as returned by the SP) in the access token step.
  431. Returns:
  432. Access token
  433. Raises:
  434. NonOAuthToken if the user's authorized request token is not an OAuth
  435. token or if an authorized request token was not available.
  436. TokenUpgradeFailed if the server responded to the request with an
  437. error.
  438. """
  439. if (authorized_request_token and
  440. not isinstance(authorized_request_token, gdata.auth.OAuthToken)):
  441. raise NonOAuthToken
  442. if not authorized_request_token:
  443. if isinstance(self.current_token, gdata.auth.OAuthToken):
  444. authorized_request_token = self.current_token
  445. else:
  446. current_scopes = lookup_scopes(self.service)
  447. if current_scopes:
  448. token = self.token_store.find_token(current_scopes[0])
  449. if isinstance(token, gdata.auth.OAuthToken):
  450. authorized_request_token = token
  451. if not authorized_request_token:
  452. raise NonOAuthToken
  453. access_token_url = gdata.auth.GenerateOAuthAccessTokenUrl(
  454. authorized_request_token,
  455. self._oauth_input_params,
  456. access_token_url=request_url,
  457. oauth_version=oauth_version,
  458. oauth_verifier=oauth_verifier)
  459. response = self.http_client.request('GET', str(access_token_url))
  460. if response.status == 200:
  461. token = gdata.auth.OAuthTokenFromHttpBody(response.read())
  462. token.scopes = authorized_request_token.scopes
  463. token.oauth_input_params = authorized_request_token.oauth_input_params
  464. self.SetOAuthToken(token)
  465. return token
  466. else:
  467. raise TokenUpgradeFailed({'status': response.status,
  468. 'reason': 'Non 200 response on upgrade',
  469. 'body': response.read()})
  470. def RevokeOAuthToken(self, request_url='%s/accounts/AuthSubRevokeToken' % \
  471. AUTH_SERVER_HOST):
  472. """Revokes an existing OAuth token.
  473. request_url: Token revoke URL. The default is
  474. 'https://www.google.com/accounts/AuthSubRevokeToken'.
  475. Raises:
  476. NonOAuthToken if the user's auth token is not an OAuth token.
  477. RevokingOAuthTokenFailed if request for revoking an OAuth token failed.
  478. """
  479. scopes = lookup_scopes(self.service)
  480. token = self.token_store.find_token(scopes[0])
  481. if not isinstance(token, gdata.auth.OAuthToken):
  482. raise NonOAuthToken
  483. response = token.perform_request(self.http_client, 'GET', request_url,
  484. headers={'Content-Type':'application/x-www-form-urlencoded'})
  485. if response.status == 200:
  486. self.token_store.remove_token(token)
  487. else:
  488. raise RevokingOAuthTokenFailed
  489. def GetAuthSubToken(self):
  490. """Returns the AuthSub token as a string.
  491. If the token is an gdta.auth.AuthSubToken, the Authorization Label
  492. ("AuthSub token") is removed.
  493. This method examines the current_token to see if it is an AuthSubToken
  494. or SecureAuthSubToken. If not, it searches the token_store for a token
  495. which matches the current scope.
  496. The current scope is determined by the service name string member.
  497. Returns:
  498. If the current_token is set to an AuthSubToken/SecureAuthSubToken,
  499. return the token string. If there is no current_token, a token string
  500. for a token which matches the service object's default scope is returned.
  501. If there are no tokens valid for the scope, returns None.
  502. """
  503. if isinstance(self.current_token, gdata.auth.AuthSubToken):
  504. return self.current_token.get_token_string()
  505. current_scopes = lookup_scopes(self.service)
  506. if current_scopes:
  507. token = self.token_store.find_token(current_scopes[0])
  508. if isinstance(token, gdata.auth.AuthSubToken):
  509. return token.get_token_string()
  510. else:
  511. token = self.token_store.find_token(atom.token_store.SCOPE_ALL)
  512. if isinstance(token, gdata.auth.ClientLoginToken):
  513. return token.get_token_string()
  514. return None
  515. def SetAuthSubToken(self, token, scopes=None, rsa_key=None):
  516. """Sets the token sent in requests to an AuthSub token.
  517. Sets the current_token and attempts to add the token to the token_store.
  518. Only use this method if you have received a token from the AuthSub
  519. service. The auth token is set automatically when UpgradeToSessionToken()
  520. is used. See documentation for Google AuthSub here:
  521. http://code.google.com/apis/accounts/AuthForWebApps.html
  522. Args:
  523. token: gdata.auth.AuthSubToken or gdata.auth.SecureAuthSubToken or string
  524. The token returned by the AuthSub service. If the token is an
  525. AuthSubToken or SecureAuthSubToken, the scope information stored in
  526. the token is used. If the token is a string, the scopes parameter is
  527. used to determine the valid scopes.
  528. scopes: list of URLs for which the token is valid. This is only used
  529. if the token parameter is a string.
  530. rsa_key: string (optional) Private key required for RSA_SHA1 signature
  531. method. This parameter is necessary if the token is a string
  532. representing a secure token.
  533. """
  534. if not isinstance(token, gdata.auth.AuthSubToken):
  535. token_string = token
  536. if rsa_key:
  537. token = gdata.auth.SecureAuthSubToken(rsa_key)
  538. else:
  539. token = gdata.auth.AuthSubToken()
  540. token.set_token_string(token_string)
  541. # If no scopes were set for the token, use the scopes passed in, or
  542. # try to determine the scopes based on the current service name. If
  543. # all else fails, set the token to match all requests.
  544. if not token.scopes:
  545. if scopes is None:
  546. scopes = lookup_scopes(self.service)
  547. if scopes is None:
  548. scopes = [atom.token_store.SCOPE_ALL]
  549. token.scopes = scopes
  550. if self.auto_set_current_token:
  551. self.current_token = token
  552. if self.auto_store_tokens:
  553. self.token_store.add_token(token)
  554. def GetClientLoginToken(self):
  555. """Returns the token string for the current token or a token matching the
  556. service scope.
  557. If the current_token is a ClientLoginToken, the token string for
  558. the current token is returned. If the current_token is not set, this method
  559. searches for a token in the token_store which is valid for the service
  560. object's current scope.
  561. The current scope is determined by the service name string member.
  562. The token string is the end of the Authorization header, it doesn not
  563. include the ClientLogin label.
  564. """
  565. if isinstance(self.current_token, gdata.auth.ClientLoginToken):
  566. return self.current_token.get_token_string()
  567. current_scopes = lookup_scopes(self.service)
  568. if current_scopes:
  569. token = self.token_store.find_token(current_scopes[0])
  570. if isinstance(token, gdata.auth.ClientLoginToken):
  571. return token.get_token_string()
  572. else:
  573. token = self.token_store.find_token(atom.token_store.SCOPE_ALL)
  574. if isinstance(token, gdata.auth.ClientLoginToken):
  575. return token.get_token_string()
  576. return None
  577. def SetClientLoginToken(self, token, scopes=None):
  578. """Sets the token sent in requests to a ClientLogin token.
  579. This method sets the current_token to a new ClientLoginToken and it
  580. also attempts to add the ClientLoginToken to the token_store.
  581. Only use this method if you have received a token from the ClientLogin
  582. service. The auth_token is set automatically when ProgrammaticLogin()
  583. is used. See documentation for Google ClientLogin here:
  584. http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html
  585. Args:
  586. token: string or instance of a ClientLoginToken.
  587. """
  588. if not isinstance(token, gdata.auth.ClientLoginToken):
  589. token_string = token
  590. token = gdata.auth.ClientLoginToken()
  591. token.set_token_string(token_string)
  592. if not token.scopes:
  593. if scopes is None:
  594. scopes = lookup_scopes(self.service)
  595. if scopes is None:
  596. scopes = [atom.token_store.SCOPE_ALL]
  597. token.scopes = scopes
  598. if self.auto_set_current_token:
  599. self.current_token = token
  600. if self.auto_store_tokens:
  601. self.token_store.add_token(token)
  602. # Private methods to create the source property.
  603. def __GetSource(self):
  604. return self.__source
  605. def __SetSource(self, new_source):
  606. self.__source = new_source
  607. # Update the UserAgent header to include the new application name.
  608. self.additional_headers['User-Agent'] = atom.http_interface.USER_AGENT % (
  609. self.__source,)
  610. source = property(__GetSource, __SetSource,
  611. doc="""The source is the name of the application making the request.
  612. It should be in the form company_id-app_name-app_version""")
  613. # Authentication operations
  614. def ProgrammaticLogin(self, captcha_token=None, captcha_response=None):
  615. """Authenticates the user and sets the GData Auth token.
  616. Login retreives a temporary auth token which must be used with all
  617. requests to GData services. The auth token is stored in the GData client
  618. object.
  619. Login is also used to respond to a captcha challenge. If the user's login
  620. attempt failed with a CaptchaRequired error, the user can respond by
  621. calling Login with the captcha token and the answer to the challenge.
  622. Args:
  623. captcha_token: string (optional) The identifier for the captcha challenge
  624. which was presented to the user.
  625. captcha_response: string (optional) The user's answer to the captch
  626. challenge.
  627. Raises:
  628. CaptchaRequired if the login service will require a captcha response
  629. BadAuthentication if the login service rejected the username or password
  630. Error if the login service responded with a 403 different from the above
  631. """
  632. request_body = gdata.auth.generate_client_login_request_body(self.email,
  633. self.password, self.service, self.source, self.account_type,
  634. captcha_token, captcha_response)
  635. # If the user has defined their own authentication service URL,
  636. # send the ClientLogin requests to this URL:
  637. if not self.auth_service_url:
  638. auth_request_url = AUTH_SERVER_HOST + '/accounts/ClientLogin'
  639. else:
  640. auth_request_url = self.auth_service_url
  641. auth_response = self.http_client.request('POST', auth_request_url,
  642. data=request_body,
  643. headers={'Content-Type':'application/x-www-form-urlencoded'})
  644. response_body = auth_response.read()
  645. if auth_response.status == 200:
  646. # TODO: insert the token into the token_store directly.
  647. self.SetClientLoginToken(
  648. gdata.auth.get_client_login_token(response_body))
  649. self.__captcha_token = None
  650. self.__captcha_url = None
  651. elif auth_response.status == 403:
  652. # Examine each line to find the error type and the captcha token and
  653. # captch URL if they are present.
  654. captcha_parameters = gdata.auth.get_captcha_challenge(response_body,
  655. captcha_base_url='%s/accounts/' % AUTH_SERVER_HOST)
  656. if captcha_parameters:
  657. self.__captcha_token = captcha_parameters['token']
  658. self.__captcha_url = captcha_parameters['url']
  659. raise CaptchaRequired, 'Captcha Required'
  660. elif response_body.splitlines()[0] == 'Error=BadAuthentication':
  661. self.__captcha_token = None
  662. self.__captcha_url = None
  663. raise BadAuthentication, 'Incorrect username or password'
  664. else:
  665. self.__captcha_token = None
  666. self.__captcha_url = None
  667. raise Error, 'Server responded with a 403 code'
  668. elif auth_response.status == 302:
  669. self.__captcha_token = None
  670. self.__captcha_url = None
  671. # Google tries to redirect all bad URLs back to
  672. # http://www.google.<locale>. If a redirect
  673. # attempt is made, assume the user has supplied an incorrect authentication URL
  674. raise BadAuthenticationServiceURL, 'Server responded with a 302 code.'
  675. def ClientLogin(self, username, password, account_type=None, service=None,
  676. auth_service_url=None, source=None, captcha_token=None,
  677. captcha_response=None):
  678. """Convenience method for authenticating using ProgrammaticLogin.
  679. Sets values for email, password, and other optional members.
  680. Args:
  681. username:
  682. password:
  683. account_type: string (optional)
  684. service: string (optional)
  685. auth_service_url: string (optional)
  686. captcha_token: string (optional)
  687. captcha_response: string (optional)
  688. """
  689. self.email = username
  690. self.password = password
  691. if account_type:
  692. self.account_type = account_type
  693. if service:
  694. self.service = service
  695. if source:
  696. self.source = source
  697. if auth_service_url:
  698. self.auth_service_url = auth_service_url
  699. self.ProgrammaticLogin(captcha_token, captcha_response)
  700. def GenerateAuthSubURL(self, next, scope, secure=False, session=True,
  701. domain='default'):
  702. """Generate a URL at which the user will login and be redirected back.
  703. Users enter their credentials on a Google login page and a token is sent
  704. to the URL specified in next. See documentation for AuthSub login at:
  705. http://code.google.com/apis/accounts/docs/AuthSub.html
  706. Args:
  707. next: string The URL user will be sent to after logging in.
  708. scope: string or list of strings. The URLs of the services to be
  709. accessed.
  710. secure: boolean (optional) Determines whether or not the issued token
  711. is a secure token.
  712. session: boolean (optional) Determines whether or not the issued token
  713. can be upgraded to a session token.
  714. """
  715. if not isinstance(scope, (list, tuple)):
  716. scope = (scope,)
  717. return gdata.auth.generate_auth_sub_url(next, scope, secure=secure,
  718. session=session,
  719. request_url='%s/accounts/AuthSubRequest' % AUTH_SERVER_HOST,
  720. domain=domain)
  721. def UpgradeToSessionToken(self, token=None):
  722. """Upgrades a single use AuthSub token to a session token.
  723. Args:
  724. token: A gdata.auth.AuthSubToken or gdata.auth.SecureAuthSubToken
  725. (optional) which is good for a single use but can be upgraded
  726. to a session token. If no token is passed in, the token
  727. is found by looking in the token_store by looking for a token
  728. for the current scope.
  729. Raises:
  730. NonAuthSubToken if the user's auth token is not an AuthSub token
  731. TokenUpgradeFailed if the server responded to the request with an
  732. error.
  733. """
  734. if token is None:
  735. scopes = lookup_scopes(self.service)
  736. if scopes:
  737. token = self.token_store.find_token(scopes[0])
  738. else:
  739. token = self.token_store.find_token(atom.token_store.SCOPE_ALL)
  740. if not isinstance(token, gdata.auth.AuthSubToken):
  741. raise NonAuthSubToken
  742. self.SetAuthSubToken(self.upgrade_to_session_token(token))
  743. def upgrade_to_session_token(self, token):
  744. """Upgrades a single use AuthSub token to a session token.
  745. Args:
  746. token: A gdata.auth.AuthSubToken or gdata.auth.SecureAuthSubToken
  747. which is good for a single use but can be upgraded to a
  748. session token.
  749. Returns:
  750. The upgraded token as a gdata.auth.AuthSubToken object.
  751. Raises:
  752. TokenUpgradeFailed if the server responded to the request with an
  753. error.
  754. """
  755. response = token.perform_request(self.http_client, 'GET',
  756. AUTH_SERVER_HOST + '/accounts/AuthSubSessionToken',
  757. headers={'Content-Type':'application/x-www-form-urlencoded'})
  758. response_body = response.read()
  759. if response.status == 200:
  760. token.set_token_string(
  761. gdata.auth.token_from_http_body(response_body))
  762. return token
  763. else:
  764. raise TokenUpgradeFailed({'status': response.status,
  765. 'reason': 'Non 200 response on upgrade',
  766. 'body': response_body})
  767. def RevokeAuthSubToken(self):
  768. """Revokes an existing AuthSub token.
  769. Raises:
  770. NonAuthSubToken if the user's auth token is not an AuthSub token
  771. """
  772. scopes = lookup_scopes(self.service)
  773. token = self.token_store.find_token(scopes[0])
  774. if not isinstance(token, gdata.auth.AuthSubToken):
  775. raise NonAuthSubToken
  776. response = token.perform_request(self.http_client, 'GET',
  777. AUTH_SERVER_HOST + '/accounts/AuthSubRevokeToken',
  778. headers={'Content-Type':'application/x-www-form-urlencoded'})
  779. if response.status == 200:
  780. self.token_store.remove_token(token)
  781. def AuthSubTokenInfo(self):
  782. """Fetches the AuthSub token's metadata from the server.
  783. Raises:
  784. NonAuthSubToken if the user's auth token is not an AuthSub token
  785. """
  786. scopes = lookup_scopes(self.service)
  787. token = self.token_store.find_token(scopes[0])
  788. if not isinstance(token, gdata.auth.AuthSubToken):
  789. raise NonAuthSubToken
  790. response = token.perform_request(self.http_client, 'GET',
  791. AUTH_SERVER_HOST + '/accounts/AuthSubTokenInfo',
  792. headers={'Content-Type':'application/x-www-form-urlencoded'})
  793. result_body = response.read()
  794. if response.status == 200:
  795. return result_body
  796. else:
  797. raise RequestError, {'status': response.status,
  798. 'body': result_body}
  799. def GetWithRetries(self, uri, extra_headers=None, redirects_remaining=4,
  800. encoding='UTF-8', converter=None, num_retries=DEFAULT_NUM_RETRIES,
  801. delay=DEFAULT_DELAY, backoff=DEFAULT_BACKOFF, logger=None):
  802. """This is a wrapper method for Get with retring capability.
  803. To avoid various errors while retrieving bulk entities by retring
  804. specified times.
  805. Note this method relies on the time module and so may not be usable
  806. by default in Python2.2.
  807. Args:
  808. num_retries: integer The retry count.
  809. delay: integer The initial delay for retring.
  810. backoff: integer how much the delay should lengthen after each failure.
  811. logger: an object which has a debug(str) method to receive logging
  812. messages. Recommended that you pass in the logging module.
  813. Raises:
  814. ValueError if any of the parameters has an invalid value.
  815. RanOutOfTries on failure after number of retries.
  816. """
  817. # Moved import for time module inside this method since time is not a
  818. # default module in Python2.2. This method will not be usable in
  819. # Python2.2.
  820. import time
  821. if backoff <= 1:
  822. raise ValueError("backoff must be greater than 1")
  823. num_retries = int(num_retries)
  824. if num_retries < 0:
  825. raise ValueError("num_retries must be 0 or greater")
  826. if delay <= 0:
  827. raise ValueError("delay must be greater than 0")
  828. # Let's start
  829. mtries, mdelay = num_retries, delay
  830. while mtries > 0:
  831. if mtries != num_retries:
  832. if logger:
  833. logger.debug("Retrying...")
  834. try:
  835. rv = self.Get(uri, extra_headers=extra_headers,
  836. redirects_remaining=redirects_remaining,
  837. encoding=encoding, converter=converter)
  838. except (SystemExit, RequestError):
  839. # Allow these errors
  840. raise
  841. except Exception, e:
  842. if logger:
  843. logger.debug(e)
  844. mtries -= 1
  845. time.sleep(mdelay)
  846. mdelay *= backoff
  847. else:
  848. # This is the right path.
  849. if logger:
  850. logger.debug("Succeeeded...")
  851. return rv
  852. raise RanOutOfTries('Ran out of tries.')
  853. # CRUD operations
  854. def Get(self, uri, extra_headers=None, redirects_remaining=4,
  855. encoding='UTF-8', converter=None):
  856. """Query the GData API with the given URI
  857. The uri is the portion of the URI after the server value
  858. (ex: www.google.com).
  859. To perform a query against Google Base, set the server to
  860. 'base.google.com' and set the uri to '/base/feeds/...', where ... is
  861. your query. For example, to find snippets for all digital cameras uri
  862. should be set to: '/base/feeds/snippets?bq=digital+camera'
  863. Args:
  864. uri: string The query in the form of a URI. Example:
  865. '/base/feeds/snippets?bq=digital+camera'.
  866. extra_headers: dictionary (optional) Extra HTTP headers to be included
  867. in the GET request. These headers are in addition to
  868. those stored in the client's additional_headers property.
  869. The client automatically sets the Content-Type and
  870. Authorization headers.
  871. redirects_remaining: int (optional) Tracks the number of additional
  872. redirects this method will allow. If the service object receives
  873. a redirect and remaining is 0, it will not follow the redirect.
  874. This was added to avoid infinite redirect loops.
  875. encoding: string (optional) The character encoding for the server's
  876. response. Default is UTF-8
  877. converter: func (optional) A function which will transform
  878. the server's results before it is returned. Example: use
  879. GDataFeedFromString to parse the server response as if it
  880. were a GDataFeed.
  881. Returns:
  882. If there is no ResultsTransformer specified in the call, a GDataFeed
  883. or GDataEntry depending on which is sent from the server. If the
  884. response is niether a feed or entry and there is no ResultsTransformer,
  885. return a string. If there is a ResultsTransformer, the returned value
  886. will be that of the ResultsTransformer function.
  887. """
  888. if extra_headers is None:
  889. extra_headers = {}
  890. if self.__gsessionid is not None:
  891. if uri.find('gsessionid=') < 0:
  892. if uri.find('?') > -1:
  893. uri += '&gsessionid=%s' % (self.__gsessionid,)
  894. else:
  895. uri += '?gsessionid=%s' % (self.__gsessionid,)
  896. server_response = self.request('GET', uri,
  897. headers=extra_headers)
  898. result_body = server_response.read()
  899. if server_response.status == 200:
  900. if converter:
  901. return converter(result_body)
  902. # There was no ResultsTransformer specified, so try to convert the
  903. # server's response into a GDataFeed.
  904. feed = gdata.GDataFeedFromString(result_body)
  905. if not feed:
  906. # If conversion to a GDataFeed failed, try to convert the server's
  907. # response to a GDataEntry.
  908. entry = gdata.GDataEntryFromString(result_body)
  909. if not entry:
  910. # The server's response wasn't a feed, or an entry, so return the
  911. # response body as a string.
  912. return result_body
  913. return entry
  914. return feed
  915. elif server_response.status == 302:
  916. if redirects_remaining > 0:
  917. location = (server_response.getheader('Location')
  918. or server_response.getheader('location'))
  919. if location is not None:
  920. m = re.compile('[\?\&]gsessionid=(\w*)').search(location)
  921. if m is not None:
  922. self.__gsessionid = m.group(1)
  923. return GDataService.Get(self, location, extra_headers, redirects_remaining - 1,
  924. encoding=encoding, converter=converter)
  925. else:
  926. raise RequestError, {'status': server_response.status,
  927. 'reason': '302 received without Location header',
  928. 'body': result_body}
  929. else:
  930. raise RequestError, {'status': server_response.status,
  931. 'reason': 'Redirect received, but redirects_remaining <= 0',
  932. 'body': result_body}
  933. else:
  934. raise RequestError, {'status': server_response.status,
  935. 'reason': server_response.reason, 'body': result_body}
  936. def GetMedia(self, uri, extra_headers=None):
  937. """Returns a MediaSource containing media and its metadata from the given
  938. URI string.
  939. """
  940. response_handle = self.request('GET', uri,
  941. headers=extra_headers)
  942. return gdata.MediaSource(response_handle, response_handle.getheader(
  943. 'Content-Type'),
  944. response_handle.getheader('Content-Length'))
  945. def GetEntry(self, uri, extra_headers=None):
  946. """Query the GData API with the given URI and receive an Entry.
  947. See also documentation for gdata.service.Get
  948. Args:
  949. uri: string The query in the form of a URI. Example:
  950. '/base/feeds/snippets?bq=digital+camera'.
  951. extra_headers: dictionary (optional) Extra HTTP headers to be included
  952. in the GET request. These headers are in addition to
  953. those stored in the client's additional_headers property.
  954. The client automatically sets the Content-Type and
  955. Authorization headers.
  956. Returns:
  957. A GDataEntry built from the XML in the server's response.
  958. """
  959. result = GDataService.Get(self, uri, extra_headers,
  960. converter=atom.EntryFromString)
  961. if isinstance(result, atom.Entry):
  962. return result
  963. else:
  964. raise UnexpectedReturnType, 'Server did not send an entry'
  965. def GetFeed(self, uri, extra_headers=None,
  966. converter=gdata.GDataFeedFromString):
  967. """Query the GData API with the given URI and receive a Feed.
  968. See also documentation for gdata.service.Get
  969. Args:
  970. uri: string The query in the form of a URI. Example:
  971. '/base/feeds/snippets?bq=digital+camera'.
  972. extra_headers: dictionary (optional) Extra HTTP headers to be included
  973. in the GET request. These headers are in addition to
  974. those stored in the client's additional_headers property.
  975. The client automatically sets the Content-Type and
  976. Authorization headers.
  977. Returns:
  978. A GDataFeed built from the XML in the server's response.
  979. """
  980. result = GDataService.Get(self, uri, extra_headers, converter=converter)
  981. if isinstance(result, atom.Feed):
  982. return result
  983. else:
  984. raise UnexpectedReturnType, 'Server did not send a feed'
  985. def GetNext(self, feed):
  986. """Requests the next 'page' of results in the feed.
  987. This method uses the feed's next link to request an additional feed
  988. and uses the class of the feed to convert the results of the GET request.
  989. Args:
  990. feed: atom.Feed or a subclass. The feed should contain a next link and
  991. the type of the feed will be applied to the results from the
  992. server. The new feed which is returned will be of the same class
  993. as this feed which was passed in.
  994. Returns:
  995. A new feed representing the next set of results in the server's feed.
  996. The type of this feed will match that of the feed argument.
  997. """
  998. next_link = feed.GetNextLink()
  999. # Create a closure which will convert an XML string to the class of
  1000. # the feed object passed in.
  1001. def ConvertToFeedClass(xml_string):
  1002. return atom.CreateClassFromXMLString(feed.__class__, xml_string)
  1003. # Make a GET request on the next link and use the above closure for the
  1004. # converted which processes the XML string from the server.
  1005. if next_link and next_link.href:
  1006. return GDataService.Get(self, next_link.href,
  1007. converter=ConvertToFeedClass)
  1008. else:
  1009. return None
  1010. def Post(self, data, uri, extra_headers=None, url_params=None,
  1011. escape_params=True, redirects_remaining=4, media_source=None,
  1012. converter=None):
  1013. """Insert or update data into a GData service at the given URI.
  1014. Args:
  1015. data: string, ElementTree._Element, atom.Entry, or gdata.GDataEntry The
  1016. XML to be sent to the uri.
  1017. uri: string The location (feed) to which the data should be inserted.
  1018. Example: '/base/feeds/items'.
  1019. extra_headers: dict (optional) HTTP headers which are to be included.
  1020. The client automatically sets the Content-Type,
  1021. Authorization, and Content-Length headers.
  1022. url_params: dict (optional) Additional URL parameters to be included
  1023. in the URI. These are translated into query arguments
  1024. in the form '&dict_key=value&...'.
  1025. Example: {'max-results': '250'} becomes &max-results=250
  1026. escape_params: boolean (optional) If false, the calling code has already
  1027. ensured that the query will form a valid URL (all
  1028. reserved characters have been escaped). If true, this
  1029. method will escape the query and any URL parameters
  1030. provided.
  1031. media_source: MediaSource (optional) Container for the media to be sent
  1032. along with the entry, if provided.
  1033. converter: func (optional) A function which will be executed on the
  1034. server's response. Often this is a function like
  1035. GDataEntryFromString which will parse the body of the server's
  1036. response and return a GDataEntry.
  1037. Returns:
  1038. If the post succeeded, this method will return a GDataFeed, GDataEntry,
  1039. or the results of running converter on the server's result body (if
  1040. converter was specified).
  1041. """
  1042. return GDataService.PostOrPut(self, 'POST', data, uri,
  1043. extra_headers=extra_headers, url_params=url_params,
  1044. escape_params=escape_params, redirects_remaining=redirects_remaining,
  1045. media_source=media_source, converter=converter)
  1046. def PostOrPut(self, verb, data, uri, extra_headers=None, url_params=None,
  1047. escape_params=True, redirects_remaining=4, media_source=None,
  1048. converter=None):
  1049. """Insert data into a GData service at the given URI.
  1050. Args:
  1051. verb: string, either 'POST' or 'PUT'
  1052. data: string, ElementTree._Element, atom.Entry, or gdata.GDataEntry The
  1053. XML to be sent to the uri.
  1054. uri: string The location (feed) to which the data should be inserted.
  1055. Example: '/base/feeds/items'.
  1056. extra_headers: dict (optional) HTTP headers which are to be included.
  1057. The client automatically sets the Content-Type,
  1058. Authorization, and Content-Length headers.
  1059. url_params: dict (optional) Additional URL parameters to be included
  1060. in the URI. These are translated into query arguments
  1061. in the form '&dict_key=value&...'.
  1062. Example: {'max-results': '250'} becomes &max-results=250
  1063. escape_params: boolean (optional) If false, the calling code has already
  1064. ensured that the query will form a valid URL (all
  1065. reserved characters have been escaped). If true, this
  1066. method will escape the query and any URL parameters
  1067. provided.
  1068. media_source: MediaSource (optional) Container for the media to be sent
  1069. along with the entry, if provided.
  1070. converter: func (optional) A function which will be executed on the
  1071. server's response. Often this is a function like
  1072. GDataEntryFromString which will parse the body of the server's
  1073. response and return a GDataEntry.
  1074. Returns:
  1075. If the post succeeded, this method will return a GDataFeed, GDataEntry,
  1076. or the results of running converter on the server's result body (if
  1077. converter was specified).
  1078. """
  1079. if extra_headers is None:
  1080. extra_headers = {}
  1081. if self.__gsessionid is not None:
  1082. if uri.find('gsessionid=') < 0:
  1083. if url_params is None:
  1084. url_params = {}
  1085. url_params['gsessionid'] = self.__gsessionid
  1086. if data and media_source:
  1087. if ElementTree.iselement(data):
  1088. data_str = ElementTree.tostring(data)
  1089. else:
  1090. data_str = str(data)
  1091. multipart = []
  1092. multipart.append('Media multipart posting\r\n--END_OF_PART\r\n' + \
  1093. 'Content-Type: application/atom+xml\r\n\r\n')
  1094. multipart.append('\r\n--END_OF_PART\r\nContent-Type: ' + \
  1095. media_source.content_type+'\r\n\r\n')
  1096. multipart.append('\r\n--END_OF_PART--\r\n')
  1097. extra_headers['MIME-version'] = '1.0'
  1098. extra_headers['Content-Length'] = str(len(multipart[0]) +
  1099. len(multipart[1]) + len(multipart[2]) +
  1100. len(data_str) + media_source.content_length)
  1101. extra_headers['Content-Type'] = 'multipart/related; boundary=END_OF_PART'
  1102. server_response = self.request(verb, uri,
  1103. data=[multipart[0], data_str, multipart[1], media_source.file_handle,
  1104. multipart[2]], headers=extra_headers, url_params=url_params)
  1105. result_body = server_response.read()
  1106. elif media_source or isinstance(data, gdata.MediaSource):
  1107. if isinstance(data, gdata.MediaSource):
  1108. media_source = data
  1109. extra_headers['Content-Length'] = str(media_source.content_length)
  1110. extra_headers['Content-Type'] = media_source.content_type
  1111. server_response = self.request(verb, uri,
  1112. data=media_source.file_handle, headers=extra_headers,
  1113. url_params=url_params)
  1114. result_body = server_response.read()
  1115. else:
  1116. http_data = data
  1117. content_type = 'application/atom+xml'
  1118. extra_headers['Content-Type'] = content_type
  1119. server_response = self.request(verb, uri, data=http_data,
  1120. headers=extra_headers, url_params=url_params)
  1121. result_body = server_response.read()
  1122. # Server returns 201 for most post requests, but when performing a batch
  1123. # request the server responds with a 200 on success.
  1124. if server_response.status == 201 or server_response.status == 200:
  1125. if converter:
  1126. return converter(result_body)
  1127. feed = gdata.GDataFeedFromString(result_body)
  1128. if not feed:
  1129. entry = gdata.GDataEntryFromString(result_body)
  1130. if not entry:
  1131. return result_body
  1132. return entry
  1133. return feed
  1134. elif server_response.status == 302:
  1135. if redirects_remaining > 0:
  1136. location = (server_response.getheader('Location')
  1137. or server_response.getheader('location'))
  1138. if location is not None:
  1139. m = re.compile('[\?\&]gsessionid=(\w*)').search(location)
  1140. if m is not None:
  1141. self.__gsessionid = m.group(1)
  1142. return GDataService.PostOrPut(self, verb, data, location,
  1143. extra_headers, url_params, escape_params,
  1144. redirects_remaining - 1, media_source, converter=converter)
  1145. else:
  1146. raise RequestError, {'status': server_response.status,
  1147. 'reason': '302 received without Location header',
  1148. 'body': result_body}
  1149. else:
  1150. raise RequestError, {'status': server_response.status,
  1151. 'reason': 'Redirect received, but redirects_remaining <= 0',
  1152. 'body': result_body}
  1153. else:
  1154. raise RequestError, {'status': server_response.status,
  1155. 'reason': server_response.reason, 'body': result_body}
  1156. def Put(self, data, uri, extra_headers=None, url_params=None,
  1157. escape_params=True, redirects_remaining=3, media_source=None,
  1158. converter=None):
  1159. """Updates an entry at the given URI.
  1160. Args:
  1161. data: string, ElementTree._Element, or xml_wrapper.ElementWrapper The
  1162. XML containing the updated data.
  1163. uri: string A URI indicating entry to which the update will be applied.
  1164. Example: '/base/feeds/items/ITEM-ID'
  1165. extra_headers: dict (optional) HTTP headers which are to be included.
  1166. The client automatically sets the Content-Type,
  1167. Authorization, and Content-Length headers.
  1168. url_params: dict (optional) Additional URL parameters to be included
  1169. in the URI. These are translated into query arguments
  1170. in the form '&dict_key=value&...'.
  1171. Example: {'max-results': '250'} becomes &max-results=250
  1172. escape_params: boolean (optional) If false, the calling code has already
  1173. ensured that the query will form a valid URL (all
  1174. reserved characters have been escaped). If true, this
  1175. method will escape the query and any URL parameters
  1176. provided.
  1177. converter: func (optional) A function which will be executed on the
  1178. server's response. Often this is a function like
  1179. GDataEntryFromString which will parse the body of the server's
  1180. response and return a GDataEntry.
  1181. Returns:
  1182. If the put succeeded, this method will return a GDataFeed, GDataEntry,
  1183. or the results of running converter on the server's result body (if
  1184. converter was specified).
  1185. """
  1186. return GDataService.PostOrPut(self, 'PUT', data, uri,
  1187. extra_headers=extra_headers, url_params=url_params,
  1188. escape_params=escape_params, redirects_remaining=redirects_remaining,
  1189. media_source=media_source, converter=converter)
  1190. def Delete(self, uri, extra_headers=None, url_params=None,
  1191. escape_params=True, redirects_remaining=4):
  1192. """Deletes the entry at the given URI.
  1193. Args:
  1194. uri: string The URI of the entry to be deleted. Example:
  1195. '/base/feeds/items/ITEM-ID'
  1196. extra_headers: dict (optional) HTTP headers which are to be included.
  1197. The client automatically sets the Content-Type and
  1198. Authorization headers.
  1199. url_params: dict (optional) Additional URL parameters to be included
  1200. in the URI. These are translated into query arguments
  1201. in the form '&dict_key=value&...'.
  1202. Example: {'max-results': '250'} becomes &max-results=250
  1203. escape_params: boolean (optional) If false, the calling code has already
  1204. ensured that the query will form a valid URL (all
  1205. reserved characters have been escaped). If true, this
  1206. method will escape the query and any URL parameters
  1207. provided.
  1208. Returns:
  1209. True if the entry was deleted.
  1210. """
  1211. if extra_headers is None:
  1212. extra_headers = {}
  1213. if self.__gsessionid is not None:
  1214. if uri.find('gsessionid=') < 0:
  1215. if url_params is None:
  1216. url_params = {}
  1217. url_params['gsessionid'] = self.__gsessionid
  1218. server_response = self.request('DELETE', uri,
  1219. headers=extra_headers, url_params=url_params)
  1220. result_body = server_response.read()
  1221. if server_response.status == 200:
  1222. return True
  1223. elif server_response.status == 302:
  1224. if redirects_remaining > 0:
  1225. location = (server_response.getheader('Location')
  1226. or server_response.getheader('location'))
  1227. if location is not None:
  1228. m = re.compile('[\?\&]gsessionid=(\w*)').search(location)
  1229. if m is not None:
  1230. self.__gsessionid = m.group(1)
  1231. return GDataService.Delete(self, location, extra_headers,
  1232. url_params, escape_params, redirects_remaining - 1)
  1233. else:
  1234. raise RequestError, {'status': server_response.status,
  1235. 'reason': '302 received without Location header',
  1236. 'body': result_body}
  1237. else:
  1238. raise RequestError, {'status': server_response.status,
  1239. 'reason': 'Redirect received, but redirects_remaining <= 0',
  1240. 'body': result_body}
  1241. else:
  1242. raise RequestError, {'status': server_response.status,
  1243. 'reason': server_response.reason, 'body': result_body}
  1244. def ExtractToken(url, scopes_included_in_next=True):
  1245. """Gets the AuthSub token from the current page's URL.
  1246. Designed to be used on the URL that the browser is sent to after the user
  1247. authorizes this application at the page given by GenerateAuthSubRequestUrl.
  1248. Args:
  1249. url: The current page's URL. It should contain the token as a URL
  1250. parameter. Example: 'http://example.com/?...&token=abcd435'
  1251. scopes_included_in_next: If True, this function looks for a scope value
  1252. associated with the token. The scope is a URL parameter with the
  1253. key set to SCOPE_URL_PARAM_NAME. This parameter should be present
  1254. if the AuthSub request URL was generated using
  1255. GenerateAuthSubRequestUrl with include_scope_in_next set to True.
  1256. Returns:
  1257. A tuple containing the token string and a list of scope strings for which
  1258. this token should be valid. If the scope was not included in the URL, the
  1259. tuple will contain (token, None).
  1260. """
  1261. parsed = urlparse.urlparse(url)
  1262. token = gdata.auth.AuthSubTokenFromUrl(parsed[4])
  1263. scopes = ''
  1264. if scopes_included_in_next:
  1265. for pair in parsed[4].split('&'):
  1266. if pair.startswith('%s=' % SCOPE_URL_PARAM_NAME):
  1267. scopes = urllib.unquote_plus(pair.split('=')[1])
  1268. return (token, scopes.split(' '))
  1269. def GenerateAuthSubRequestUrl(next, scopes, hd='default', secure=False,
  1270. session=True, request_url='https://www.google.com/accounts/AuthSubRequest',
  1271. include_scopes_in_next=True):
  1272. """Creates a URL to request an AuthSub token to access Google services.
  1273. For more details on AuthSub, see the documentation here:
  1274. http://code.google.com/apis/accounts/docs/AuthSub.html
  1275. Args:
  1276. next: The URL where the browser should be sent after the user authorizes
  1277. the application. This page is responsible for receiving the token
  1278. which is embeded in the URL as a parameter.
  1279. scopes: The base URL to which access will be granted. Example:
  1280. 'http://www.google.com/calendar/feeds' will grant access to all
  1281. URLs in the Google Calendar data API. If you would like a token for
  1282. multiple scopes, pass in a list of URL strings.
  1283. hd: The domain to which the user's account belongs. This is set to the
  1284. domain name if you are using Google Apps. Example: 'example.org'
  1285. Defaults to 'default'
  1286. secure: If set to True, all requests should be signed. The default is
  1287. False.
  1288. session: If set to True, the token received by the 'next' URL can be
  1289. upgraded to a multiuse session token. If session is set to False, the
  1290. token may only be used once and cannot be upgraded. Default is True.
  1291. request_url: The base of the URL to which the user will be sent to
  1292. authorize this application to access their data. The default is
  1293. 'https://www.google.com/accounts/AuthSubRequest'.
  1294. include_scopes_in_next: Boolean if set to true, the 'next' parameter will
  1295. be modified to include the requested scope as a URL parameter. The
  1296. key for the next's scope parameter will be SCOPE_URL_PARAM_NAME. The
  1297. benefit of including the scope URL as a parameter to the next URL, is
  1298. that the page which receives the AuthSub token will be able to tell
  1299. which URLs the token grants access to.
  1300. Returns:
  1301. A URL string to which the browser should be sent.
  1302. """
  1303. if isinstance(scopes, list):
  1304. scope = ' '.join(scopes)
  1305. else:
  1306. scope = scopes
  1307. if include_scopes_in_next:
  1308. if next.find('?') > -1:
  1309. next += '&%s' % urllib.urlencode({SCOPE_URL_PARAM_NAME:scope})
  1310. else:
  1311. next += '?%s' % urllib.urlencode({SCOPE_URL_PARAM_NAME:scope})
  1312. return gdata.auth.GenerateAuthSubUrl(next=next, scope=scope, secure=secure,
  1313. session=session, request_url=request_url, domain=hd)
  1314. class Query(dict):
  1315. """Constructs a query URL to be used in GET requests
  1316. Url parameters are created by adding key-value pairs to this object as a
  1317. dict. For example, to add &max-results=25 to the URL do
  1318. my_query['max-results'] = 25
  1319. Category queries are created by adding category strings to the categories
  1320. member. All items in the categories list will be concatenated with the /
  1321. symbol (symbolizing a category x AND y restriction). If you would like to OR
  1322. 2 categories, append them as one string with a | between the categories.
  1323. For example, do query.categories.append('Fritz|Laurie') to create a query
  1324. like this feed/-/Fritz%7CLaurie . This query will look for results in both
  1325. categories.
  1326. """
  1327. def __init__(self, feed=None, text_query=None, params=None,
  1328. categories=None):
  1329. """Constructor for Query
  1330. Args:
  1331. feed: str (optional) The path for the feed (Examples:
  1332. '/base/feeds/snippets' or 'calendar/feeds/jo@gmail.com/private/full'
  1333. text_query: str (optional) The contents of the q query parameter. The
  1334. contents of the text_query are URL escaped upon conversion to a URI.
  1335. params: dict (optional) Parameter value string pairs which become URL
  1336. params when translated to a URI. These parameters are added to the
  1337. query's items (key-value pairs).
  1338. categories: list (optional) List of category strings which should be
  1339. included as query categories. See
  1340. http://code.google.com/apis/gdata/reference.html#Queries for
  1341. details. If you want to get results from category A or B (both
  1342. categories), specify a single