Example #1
0
def _GetPersistentCredentials():
    """Read persistent credentials from ~/.config/earthengine.

  Raises EEException with helpful explanation if credentials don't exist.

  Returns:
    OAuth2Credentials built from persistently stored refresh_token
  """
    try:
        tokens = json.load(open(OAuthInfo.credentials_path()))
        refresh_token = tokens["refresh_token"]
        return oauth2client.client.OAuth2Credentials(
            None,
            OAuthInfo.CLIENT_ID,
            OAuthInfo.CLIENT_SECRET,
            refresh_token,
            None,
            "https://accounts.google.com/o/oauth2/token",
            None,
        )
    except IOError:
        script = os.path.join(os.path.dirname(os.path.realpath(__file__)), "authenticate.py")
        raise EEException(
            "Please authorize access to your Earth Engine account "
            + "by running\n\n%s %s\n\nand then retry." % (sys.executable, script)
        )
def _GetPersistentCredentials():
    """Read persistent credentials from ~/.config/earthengine.

  Raises EEException with helpful explanation if credentials don't exist.

  Returns:
    OAuth2Credentials built from persistently stored refresh_token
  """
    try:
        tokens = json.load(open(OAuthInfo.credentials_path()))
        refresh_token = tokens['refresh_token']
        return oauth2client.client.OAuth2Credentials(
            None, OAuthInfo.CLIENT_ID, OAuthInfo.CLIENT_SECRET, refresh_token,
            None, 'https://accounts.google.com/o/oauth2/token', None)
    except IOError:
        raise EEException(
            'Please authorize access to your Earth Engine account '
            'by running\n\nearthengine authenticate\n\nin your '
            'command line, and then retry.')
Example #3
0
def _GetPersistentCredentials():
    """Read persistent credentials from ~/.config/earthengine.

  Raises EEException with helpful explanation if credentials don't exist.

  Returns:
    OAuth2Credentials built from persistently stored refresh_token
  """
    try:
        tokens = json.load(open(OAuthInfo.credentials_path()))
        refresh_token = tokens['refresh_token']
        return oauth2client.client.OAuth2Credentials(
            None, OAuthInfo.CLIENT_ID, OAuthInfo.CLIENT_SECRET, refresh_token,
            None, 'https://accounts.google.com/o/oauth2/token', None)
    except IOError:
        script = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                              'authenticate.py')
        raise EEException(
            'Please authorize access to your Earth Engine account ' +
            'by running\n\npython %s\n\nand then retry.' % script)
Example #4
0
      'redirect_uri': redirect_uri,
      'grant_type': 'authorization_code'
  }

  refresh_token = None
  try:
    response = urllib2.urlopen('https://accounts.google.com/o/oauth2/token',
                               urllib.urlencode(token_request_params)).read()
    tokens = json.loads(response)
    refresh_token = tokens['refresh_token']
  except urllib2.HTTPError, e:
    raise Exception('Problem requesting tokens.  Please try again.  %s %s' %
                    (e, e.read()))

  ### Write refresh token to filesystem for later use

  credentials_path = OAuthInfo.credentials_path()
  dirname = os.path.dirname(credentials_path)
  try:
    os.makedirs(dirname)
  except OSError, e:
    if e.errno != errno.EEXIST:
      raise Exception('Error creating %s: %s' % (dirname, e))

  json.dump({'refresh_token': refresh_token}, open(credentials_path, 'w'))

  print '\nSuccessfully saved authorization to %s' % credentials_path

if __name__ == '__main__':
  main()
Example #5
0
    refresh_token = None
    try:
        response = urllib2.urlopen(
            'https://accounts.google.com/o/oauth2/token',
            urllib.urlencode(token_request_params)).read()
        tokens = json.loads(response)
        refresh_token = tokens['refresh_token']
    except urllib2.HTTPError, e:
        raise Exception(
            'Problem requesting tokens.  Please try again.  %s %s' %
            (e, e.read()))

    ### Write refresh token to filesystem for later use

    credentials_path = OAuthInfo.credentials_path()
    dirname = os.path.dirname(credentials_path)
    try:
        os.makedirs(dirname)
    except OSError, e:
        if e.errno != errno.EEXIST:
            raise Exception('Error creating %s: %s' % (dirname, e))

    json.dump({'refresh_token': refresh_token}, open(credentials_path, 'w'))

    print '\nSuccessfully saved authorization to %s' % credentials_path


if __name__ == '__main__':
    main()