Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions R/auth.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@ run_ee_oauth <- function() {
load_test <- try(source_python(file = oauth_func_path), silent = T)
count <- count + 1
}
request_ee_code()
verify <- request_ee_code()

code <-
readline("Enter authorisation code for Earth Engine API here: ")

test <- try(request_ee_token(code), silent = T)
test <- try(request_ee_token(code, verify), silent = T)

while (class(test) == "try-error") {
cat(
"Problem with Authentication key input. \nPlease follow the authentication steps in the browser and copy paste the authentication token into the R console again."
)
request_ee_code()
verify <- request_ee_code()
code <- readline("enter authorisation code here: ")
test <- try(request_ee_token(code), silent = T)
test <- try(request_ee_token(code, verify), silent = T)
}
cat("Earth Engine Python API is authenticated \n")
}
Expand Down
26 changes: 22 additions & 4 deletions inst/Python/ee_authorisation_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,38 @@
import urllib
from ee.oauth import get_credentials_path
import json
import os
import base64
import hashlib

ft_scope = 'https://www.googleapis.com/auth/fusiontables'


def request_ee_code():
# get authorisation url
auth_url = ee.oauth.get_authorization_url()
out = create_codes()
code_verifier = out[0]
code_challenge = out[1]
# get authorisation url
auth_url = ee.oauth.get_authorization_url(code_challenge)
# call auth_url in browser to grand access by the user
webbrowser.open_new(auth_url)
return code_verifier

def request_ee_token(auth_code):
token = ee.oauth.request_token(auth_code)
def request_ee_token(auth_code, code_verifier):
token = ee.oauth.request_token(auth_code, code_verifier)
ee.oauth.write_token(token)


def _base64param(byte_string):
"""Encodes bytes for use as a URL parameter."""
return base64.urlsafe_b64encode(byte_string).rstrip(b'=')

def create_codes():
code_verifier = _base64param(os.urandom(32))
code_challenge = _base64param(hashlib.sha256(code_verifier).digest())
return code_verifier, code_challenge





Expand Down