{
"metadata": {
"kernelspec": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"display_name": "IPython (Python 2)",
"language": "python",
"name": "python2"
},
"name": "",
"signature": "sha256:c0f0be7808f808bfc06881b108d253f7bb14d9876f4a66c2451c79f9cf2c465d"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Configure authentication to Earth Engine"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Declare an authentication token that was generated outside of the IPython Notebook using the instructions found in the Setting Up Authentication Credentials section of the [Python API Installation and Access](https://sites.google.com/site/earthengineapidocs/python-api) page of the Earth Engine API documentation."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import ee\n",
"import errno\n",
"import json\n",
"import os\n",
"import urllib\n",
"import urllib2\n",
"from IPython.display import HTML\n",
"from IPython.display import display\n",
"from ee.oauthinfo import OAuthInfo\n",
"\n",
"# This URI prompts user to copy and paste a code after successful \n",
"# authorization. \n",
"ee_redirect_uri = 'urn:ietf:wg:oauth:2.0:oob'\n",
"\n",
"\n",
"def create_auth_url():\n",
" # TODO(user): Add an additional, non-commandline flow for iPython notebook \n",
" # for added convenience, and to work in notebook environments where \n",
" # commandline isn't available. \n",
"\n",
" # This implements the flow from: \n",
" # https://developers.google.com/accounts/docs/OAuth2ForDevices \n",
"\n",
" ### Request authorization from user \n",
"\n",
" auth_request_params = {\n",
" 'scope': OAuthInfo.SCOPE,\n",
" 'redirect_uri': ee_redirect_uri,\n",
" 'response_type': 'code',\n",
" 'client_id': OAuthInfo.CLIENT_ID\n",
" }\n",
" auth_request_url = ('https://accounts.google.com/o/oauth2/auth?' +\n",
" urllib.urlencode(auth_request_params))\n",
"\n",
" return auth_request_url\n",
"\n",
"\n",
"def ee_authenticate(auth_code):\n",
" token_request_params = {\n",
" 'code': auth_code,\n",
" 'client_id': OAuthInfo.CLIENT_ID,\n",
" 'client_secret': OAuthInfo.CLIENT_SECRET,\n",
" 'redirect_uri': ee_redirect_uri,\n",
" 'grant_type': 'authorization_code'\n",
" }\n",
"\n",
" refresh_token = None\n",
" try:\n",
" response = urllib2.urlopen('https://accounts.google.com/o/oauth2/token',\n",
" urllib.urlencode(token_request_params)).read()\n",
" tokens = json.loads(response)\n",
" refresh_token = tokens['refresh_token']\n",
" except urllib2.HTTPError, e:\n",
" raise Exception('Problem requesting tokens. Please try again. %s %s' %\n",
" (e, e.read()))\n",
"\n",
" ### Write refresh token to filesystem for later use \n",
"\n",
" credentials_path = OAuthInfo.credentials_path()\n",
" dirname = os.path.dirname(credentials_path)\n",
" try:\n",
" os.makedirs(dirname)\n",
" except OSError, e:\n",
" if e.errno != errno.EEXIST:\n",
" raise Exception('Error creating %s: %s' % (dirname, e))\n",
"\n",
" json.dump({'refresh_token': refresh_token}, open(credentials_path, 'w'))\n",
"\n",
" print '\\nSuccessfully saved authorization to %s' % credentials_path\n",
" ee.Initialize()\n",
" return True\n",
"\n",
"def auth_html():\n",
" return HTML(\n",
"(\"\"\"You need to authorize access to your Earth Engine account.
\n",
"Please follow this link, \"\"\" % create_auth_url()) +\n",
"\n",
"\"\"\"and paste the token you receive after authorization here: \n",
"\n",
" \n",
"\n",
"\n",
"\"\"\")\n",
"\n",
"# For now, call ee_initialize() instead of ee.Initialize()\n",
"def ee_initialize():\n",
" try:\n",
" ee.Initialize()\n",
" display(HTML(\"\"\"Authentication successful!\"\"\"))\n",
" except ee.EEException, e:\n",
" display(auth_html())"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}