Beispiel #1
0
  def get(self):
    verifier = self.request.get('oauth_verifier')
    request_token_key = self.request.get('oauth_token')
    if not verifier or not request_token_key:
      # user declined
      self.finish(None)
      return

    # look up the request token
    request_token = models.OAuthRequestToken.get_by_id(request_token_key)
    if request_token is None:
      raise exc.HTTPBadRequest('Invalid oauth_token: %s' % request_token_key)

    # generate and store the final token
    tp = tumblpy.Tumblpy(app_key=appengine_config.TUMBLR_APP_KEY,
                         app_secret=appengine_config.TUMBLR_APP_SECRET,
                         oauth_token=request_token_key,
                         oauth_token_secret=request_token.token_secret)
    auth_token = tp.get_authorized_tokens(verifier)
    auth_token_key = auth_token['oauth_token']
    auth_token_secret = auth_token['oauth_token_secret']

    # get the user's blogs
    # http://www.tumblr.com/docs/en/api/v2#user-methods
    tp = TumblrAuth._api_from_token(auth_token_key, auth_token_secret)
    logging.debug('Fetching user/info')
    try:
      resp = tp.post('user/info')
    except BaseException, e:
      handlers.interpret_http_exception(e)
      raise
Beispiel #2
0
 def urlopen(self, url, **kwargs):
   uri, headers, body = self.api().sign(url, **kwargs)
   try:
     return urllib2.urlopen(urllib2.Request(uri, body, headers))
   except BaseException, e:
     handlers.interpret_http_exception(e)
     raise
Beispiel #3
0
def signed_urlopen(url, token_key, token_secret, headers=None, **kwargs):
  """Wraps urllib2.urlopen() and adds an OAuth signature.
  """
  if headers is None:
    headers = {}

  # if this is a post, move the body params into the URL. Tweepy's OAuth
  # signing doesn't work if they're in the body; Twitter returns a 401.
  data = kwargs.get('data')
  if data:
    method = 'POST'
    url += ('&' if '?' in url else '?') + data
    kwargs['data'] = ''
  else:
    method = 'GET'

  headers.update(auth_header(url, token_key, token_secret, method=method))
  timeout = kwargs.pop('timeout', appengine_config.HTTP_TIMEOUT)
  logging.debug('Fetching %s', url)
  try:
    return urllib2.urlopen(urllib2.Request(url, headers=headers, **kwargs),
                           timeout=timeout)
  except BaseException, e:
    handlers.interpret_http_exception(e)
    raise
Beispiel #4
0
 def post(self, *args, **kwargs):
   """Wraps requests.post() and adds an OAuth signature.
   """
   oauth1 = twitter_auth.auth(self.token_key, self.token_secret)
   resp = requests.post(*args, auth=oauth1, **kwargs)
   try:
     resp.raise_for_status()
   except BaseException, e:
     handlers.interpret_http_exception(e)
     raise
Beispiel #5
0
 def get(self):
     state = self.request.get("state")
     blogger = BloggerV2Auth.api_from_creds(oauth_decorator.credentials)
     try:
         blogs = blogger.get_blogs()
     except BaseException, e:
         # this api call often returns 401 Unauthorized for users who aren't
         # signed up for blogger and/or don't have any blogs.
         handlers.interpret_http_exception(e)
         # we can't currently intercept declines for Google+ or Blogger, so the
         # only time we return a None auth entity right now is on error.
         self.finish(None, state=state)
         return
Beispiel #6
0
      def get(self):
        assert (appengine_config.GOOGLE_CLIENT_ID and
                appengine_config.GOOGLE_CLIENT_SECRET), (
          "Please fill in the google_client_id and google_client_secret files in "
          "your app's root directory.")

        # get the current user
        init_json_service()
        try:
          user = json_service.people().get(userId='me')\
              .execute(oauth_decorator.http())
        except BaseException, e:
          handlers.interpret_http_exception(e)
          raise
Beispiel #7
0
 def urlopen(self, url, **kwargs):
   """Wraps urllib2.urlopen() and adds OAuth credentials to the request.
   """
   kwargs.setdefault('headers', {})['authorization'] = \
       'Bearer ' + self.access_token_str
   logging.info('Fetching %s', url)
   data = kwargs.get('data')
   if data:
     logging.info('...with data: %r', data)
   try:
     return urllib2.urlopen(urllib2.Request(url, **kwargs),
                            timeout=appengine_config.HTTP_TIMEOUT)
   except BaseException, e:
     handlers.interpret_http_exception(e)
     raise
Beispiel #8
0
  def get(self):
    oauth_token = self.request.get('oauth_token')
    oauth_verifier = self.request.get('oauth_verifier')
    request_token = models.OAuthRequestToken.get_by_id(oauth_token)

    client = oauthlib.oauth1.Client(
      appengine_config.FLICKR_APP_KEY,
      client_secret=appengine_config.FLICKR_APP_SECRET,
      resource_owner_key=oauth_token,
      resource_owner_secret=request_token.token_secret,
      verifier=oauth_verifier,
      signature_type=oauthlib.oauth1.SIGNATURE_TYPE_QUERY)

    uri, headers, body = client.sign(ACCESS_TOKEN_URL)
    try:
      resp = urllib2.urlopen(uri)
    except BaseException, e:
      handlers.interpret_http_exception(e)
      raise
Beispiel #9
0
    def urlopen_access_token(url, access_token, api_key=None, **kwargs):
        """Wraps urllib2.urlopen() and adds an access_token query parameter.

    Kwargs are passed through to urlopen().
    """
        log_params = [("access_token", access_token[:4] + "...")]
        real_params = [("access_token", access_token)]
        if api_key:
            log_params.append(("api_key", api_key[:4] + "..."))
            real_params.append(("api_key", api_key))
        log_url = util.add_query_params(url, log_params)
        logging.info("Fetching %s", log_url)
        url = util.add_query_params(url, real_params)
        if "timeout" not in kwargs:
            kwargs["timeout"] = appengine_config.HTTP_TIMEOUT

        try:
            return urllib2.urlopen(url, **kwargs)
        except BaseException, e:
            handlers.interpret_http_exception(e)
            raise
Beispiel #10
0
  def get(self):
    if facebook.CallbackHandler.handle_error(self):
      return

    auth_code = self.request.get('code')
    assert auth_code

    # http://instagram.com/developer/authentication/
    data = {
      'client_id': appengine_config.INSTAGRAM_CLIENT_ID,
      'client_secret': appengine_config.INSTAGRAM_CLIENT_SECRET,
      'code': auth_code,
      'redirect_uri': self.request_url_with_state(),
      'grant_type': 'authorization_code',
    }

    logging.debug('Fetching: %s with data %s', GET_ACCESS_TOKEN_URL, data)
    try:
      resp = urllib2.urlopen(GET_ACCESS_TOKEN_URL, data=urllib.urlencode(data),
                             timeout=HTTP_TIMEOUT).read()
    except BaseException, e:
      handlers.interpret_http_exception(e)
      raise