<jupyter_start><jupyter_text>Create Access Token Notebook- The Hackathon team has registered a client called hack_client. - Use this client_id to create an access token you can use in the Jupyter environment or externally for API calls to SAS Viya.- Only one access token is needed for each team member and it is placed in the /home/<EMAIL>/access_token.txt file.- Additional access token information is found at the end of this notebook.- Find detailed information about client registration and access tokens in this blog post: [Authentication to SAS Viya: a couple of approaches](https://blogs.sas.com/content/sgf/2021/09/24/authentication-to-sas-viya/) Run the cell below and follow the resulting instructions.<jupyter_code>library('httr')

# reading environment url
codeURL <- readLines("/home/shared_data/Authorization_Code_URL.txt", warn=F)
baseURL <- unlist(strsplit(codeURL, '/'))[3]

print(glue::glue("* Please visit the following site {codeURL}"))
print(glue::glue("* At the login prompt, add your SAS login Hackathon credentials"))
print(glue::glue("* Once authenticated, you'll be redirected to an authoriztion screen, check all of the boxes that appear"))
print(glue::glue("* This will result in a short string of numbers and letters such as `VAxVFVEnKr`; this is your authorization code; copy the code"))
code <- readline("Please enter the code you generated through the previous instructions: ")

# create API URL
url <- glue::glue("https://{baseURL}/SASLogon/oauth/token?grant_type=authorization_code&code={code}")

# make API call to generate access token
response <- GET(
            url = url,
            add_headers(
            "Accept"="application/json",
            "Content-Type" = "application/x-www-form-urlencoded",
            "Authorization" = "Basic aGFja19jbGllbnQ6aGFja19zZWNyZXQ="
            )
)

# parse results and create token variable
access_token <- jsonlite::fromJSON(content(response, as = "text"))$access_token
print(jsonlite::fromJSON(content(response, as = "text")))

directory <- getwd()
writeLines(access_token, glue::glue("{directory}/access_token.txt"))

print(glue::glue("The access token was stored for you as {directory}/access_token.txt"))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10796221322537113, 'fraction_numerical': 0.006297795771479982, 'mean_word_length': 4.89920424403183, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 6, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '315391', 'license': 'Apache-2.0', 'n_tokens_mistral': 662, 'n_tokens_neox': 633, 'n_words': 223, 'path': 'connection-python-and-r-scripts/Create_Access_Token_R.ipynb', 'repo_name': 'sascommunities/sas-hackathon-2022', 'size': '2230'}