Skip to content

Commit

Permalink
Improve the message and small code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
gena committed Jun 26, 2022
1 parent a9ff5b3 commit db3ae55
Showing 1 changed file with 48 additions and 49 deletions.
97 changes: 48 additions & 49 deletions ee_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
# https://github.com/googleapis/google-api-python-client/issues/299
logging.getLogger('googleapiclient.discovery_cache').setLevel(logging.ERROR)

SCOPES = [
"https://www.googleapis.com/auth/earthengine",
"https://www.googleapis.com/auth/devstorage.full_control",
"https://www.googleapis.com/auth/accounts.reauth"
]

class MyHandler(BaseHTTPRequestHandler):
"""
Expand All @@ -27,69 +32,63 @@ def do_GET(self):
self.end_headers()
self.wfile.write(bytes("QGIS Google Earth Engine plugin authentication finished successfully.", 'utf-8'))


def authenticate(ee=None):
"""
Authenticates Google Earth Engine
"""
if ee is None:
import ee

msg = 'This plugin uses Google Earth Engine API and it requires'\
'users to be authorized, you must have a Google account for that.\n\n'\
'Click OK and follow the instructions on the web page.'
# show a dialog to allow users to start or cancel the authentication process
msg = 'This plugin uses Google Earth Engine API and it looks like it is not yet\n'\
'authenticated on this machine. You need to have a Google account\n'\
'registered in Google Earth Engine to continue\n\n'\
'Click OK to open a web browser and start the authentication process\n'\
'or click Cancel to stop the authentication process.'
reply = QMessageBox.question(None, 'Google Earth Engine plugin',
msg, QMessageBox.Ok, QMessageBox.Cancel)

if reply == QMessageBox.Ok:
# start the authentication process with Google Earth Engine

SCOPES = [
"https://www.googleapis.com/auth/earthengine",
"https://www.googleapis.com/auth/devstorage.full_control",
"https://www.googleapis.com/auth/accounts.reauth"
]
if reply == QMessageBox.Cancel:
return False

# get user login & consent
request_args = {
'response_type': 'code',
'client_id': ee.oauth.CLIENT_ID,
'redirect_uri': "http://localhost:8085/",
'scope': ' '.join(SCOPES),
'access_type': 'offline'
}
auth_url = 'https://accounts.google.com/o/oauth2/auth/oauthchooseaccount?' + urllib.parse.urlencode(
request_args)
webbrowser.open_new(auth_url)
print('Starting Google Earth Engine Authorization ...')
# start the authentication, getting user login & consent
request_args = {
'response_type': 'code',
'client_id': ee.oauth.CLIENT_ID,
'redirect_uri': "http://localhost:8085/",
'scope': ' '.join(SCOPES),
'access_type': 'offline'
}
auth_url = 'https://accounts.google.com/o/oauth2/auth/oauthchooseaccount?' \
+ urllib.parse.urlencode(request_args)
webbrowser.open_new(auth_url)
print('Starting Google Earth Engine Authorization ...')

server = HTTPServer(('localhost', 8085), MyHandler)
server.handle_request()
server = HTTPServer(('localhost', 8085), MyHandler)
server.handle_request()

if not MyHandler.auth_code:
print('QGIS EE Plugin authentication failed, can not get authentication code')
return False
if not MyHandler.auth_code:
print('QGIS EE Plugin authentication failed, can not get authentication code')
return False

# get refresh token
request_args = {
'code': MyHandler.auth_code,
'client_id': ee.oauth.CLIENT_ID,
'client_secret': ee.oauth.CLIENT_SECRET,
'redirect_uri': "http://localhost:8085/",
'grant_type': 'authorization_code',
}
# get refresh token
request_args = {
'code': MyHandler.auth_code,
'client_id': ee.oauth.CLIENT_ID,
'client_secret': ee.oauth.CLIENT_SECRET,
'redirect_uri': "http://localhost:8085/",
'grant_type': 'authorization_code',
}

data = urllib.parse.urlencode(request_args).encode()
response = urllib.request.urlopen(ee.oauth.TOKEN_URI, data).read().decode()
refresh_token = json.loads(response)['refresh_token']
data = urllib.parse.urlencode(request_args).encode()
response = urllib.request.urlopen(ee.oauth.TOKEN_URI, data).read().decode()
refresh_token = json.loads(response)['refresh_token']

# write refresh token
client_info = {}
client_info['refresh_token'] = refresh_token
client_info['scopes'] = SCOPES
ee.oauth.write_private_json(ee.oauth.get_credentials_path(), client_info)
print('QGIS EE Plugin authenticated successfully')
return True
else:
return False
# write refresh token
client_info = {}
client_info['refresh_token'] = refresh_token
client_info['scopes'] = SCOPES
ee.oauth.write_private_json(ee.oauth.get_credentials_path(), client_info)
print('QGIS EE Plugin authenticated successfully')

return True

0 comments on commit db3ae55

Please sign in to comment.