TC7.33: How to Access MyKPI from R-studio and/or Python, from your Data Analytics

×

Warning message

You can't delete this newsletter because it has not been sent to all its subscribers.
if you are a developer creating a mobile app, or developing a data anaytics on R Studio as well as on Python or other, you may need to get the access toaken to be athorized to access to your data on the platform. This page is presenting the mechanism and an example in RStudio to get the Access Token ans using it. The access token is needed in accessing to private data, while for accessing public data via smart city API it is not needed.
 
To get the accessToken you have to call
With POST call with the following parameters:
{
"client_id": "<client id depending on the App kind>", 
"grant_type": "password",
"username": "<username>" ,
"password": "<password>"
}
 
Username and Password are those of your Snap4City account. While, "<client id depending on the App kind>"  has to be obtained from Snap4City organization by sending an email to snap4city@disit.org.
 
The answer is a JSON of this kind:
{
"access_token": "eyJhbGciOiJSUzI1NiIsInIgOiAiSldUIiwia2lkIiA6ICJRczF6Wko2REIyTlp0ajB3RlVHNlFfN2JwZGhuRnRFX1NMZFdHb29CSUFFIn0.eyJqdGkiOiJjYzE4MzhjNi1iZDA4LTQyOWQtYTU3ZS04MWUyOTk4OWVkOTciLCJleHAiOjE1OTc4MjU3NzEsIm5iZiI6MCwiaWF0IjoxNTk3ODIzOTcxLCJpc3MiOiJodHRwOi8vd3d3LmRpc2l0Lm9yZy9hdXRoL3JlYWxtcy9tYXN0ZXIiLCJhdWQiOiJraWJhbmEtc3NvLXRlc3QiLCJzdWIiOiJkM2ZjNGYxMi03ZDBhLTQ2ZDEtYWIyYi0zNTM2ODViZmIyYmUiLCJ0eXAiOi..............omissis...........yZWF0ZS1yZWFsbSIsIkFyZWFNYW5hZ2VyIiwib2ZmbGluZV9hY2Nlc3MiLCJSb290QWRtaW4iXSwicHJlZmVycmVkX3VzZXJuYW1lIjoiYmFkaWkiLCJlbWFpbCI6ImNsYXVkaW8uYmFkaWlAdW5pZmkuaXQifQ.QVUdR6D5YjnhPPPSkxkvHmBvqxGaRSwxyBGfse0mUqNDfZ4ffo6Mzl0air7qPiPMf4ZI_kryJRffDgZrSbZS0LYmLI16akQGEOgVUfOLNW3H8xoh-NaQ_IrKDwHbybRAHVad1cW-CysRd9hAkA9vslWp9mv2R2bcxACd_gProvsD6HdunIyRVe4ryeRq09Ynr58Yz5nY6WwQ1sElwkv280nRtAHz0x3QN_UAqhnz9S9GHAudgChRL01Dw-yxKNTii-7hOY7wePoEnIVDOzcuY-QUYYQiEVmJ62u39tx2ojBrfcQ6bHWTXpA0iQDu2myZCOmPpMnLpTiBccrHaJPryg",
"expires_in": 1800,
"refresh_expires_in": 1800,
"refresh_token": "eyJhbGciOiJSUzI1NR5cCIgOiAiSldUIiwia2lkIiA6ICJRczF6Wko2REIyTlp0ajB3RlVHNlFfN2JwZGhuRnRFX1NMZFdHb29CSUFFIn0.eyJqdGkiOiJjZDMxYWVkMS05YmJlLTQ1ZTMtYTUzMC1jZDdlMTI5ZGI5ZjAiLCJleHAiOjE1OTc4MjU3NzEsIm5iZiI6MCwiaWF0IjoxNTk3ODIzOTcxLCJpc3MiOiJodHRwOi8vd3d3LmRpc2l0Lm9yZy9hdXRoL3JlYWxtcy9tYXN0ZXIiLCJhdWQiOiJraWJhbmEtc3NvLXRlc3QiLCJzdWIiOiJkM2ZjNGYxMi03ZDBhLTQ2ZDEtYWIyYi0zNTM2ODViZmIyYmUiLCJ0eXAiOiJSZWZyZXNoIiwi...............omissis................zVLtPDI19EaT0020_KnPETAylC2agbY6_TuJMrWAzLcPm0h2NKtWuBDTARFHHt3Dpyo_KizIqXdjibuWn0iZWeMsavM7FKwACGWlUlO7uXXK2vdgYPC4tAy_JdxzQh4cplWlfuEy-zQXz5kSBAVDmVYA-0VCgim1C52UCSQHxHcGRIDGIeqKmR1wm9qCNISR5fFOGsXvCgxHvG4O_1qXbMSUvRt4JE4-RMZtscI5k2xOZAI8Q",
"token_type": "bearer",
"not-before-policy": 1554127150,
"session_state": "09506487-bf74-4736-b958-f594b9041916"
}
 
if  in the first call we have a refresh_token saved in the APP/Rstudio it is possible to call the same URL:
With POST call with the following parameters:
{
"client_id": "<client id depending on the App kind>",  
"grant_type": "refresh_token",
"scope": "openid profile",
"refresh_token": "<please pass here the refresh_token>"
}
 
The answer is a JSON of this kind:
{
"access_token": "eyJhbGciOiJSUzI1NiIsInIgOiAiSldUIiwia2lkIiA6ICJRczF6Wko2REIyTlp0ajB3RlVHNlFfN2JwZGhuRnRFX1NMZFdHb29CSUFFIn0.eyJqdGkiOiJjYzE4MzhjNi1iZDA4LTQyOWQtYTU3ZS04MWUyOTk4OWVkOTciLCJleHAiOjE1OTc4MjU3NzEsIm5iZiI6MCwiaWF0IjoxNTk3ODIzOTcxLCJpc3MiOiJodHRwOi8vd3d3LmRpc2l0Lm9yZy9hdXRoL3JlYWxtcy9tYXN0ZXIiLCJhdWQiOiJraWJhbmEtc3NvLXRlc3QiLCJzdWIiOiJkM2ZjNGYxMi03ZDBhLTQ2ZDEtYWIyYi0zNTM2ODViZmIyYmUiLCJ0eXAiOiJCZWFyZXIiLCJhenAiOiJraWJhbmEtc3NvLXRlc3QiLCJhdXRoX3RpbWUiOjAsInNlc3Npb25fc3RhdGUiOiIwOTUwNjQ4Ny1iZjc0LTQ3MzYtYjk1OC1mNTk0YjkwNDE5MTYiLCJhY3IiOiIxIiwiYWxsb3dlZC1vcmlnaW5zIjpbXSwic................omissis................ZXJ5LWdyb3VwcyJdfSwiYWNjb3VudCI6eyJyb2xlcyI6WyJtYW5hZ2UtYWNjb3VudCIsIm1hbmFnZS1hY2NvdW50LWxpbmtzIiwidmlldy1wcm9maWxlIl19fSwicm9sZXMiOlsidW1hX2F1dGhvcml6YXRpb24iLCJhZG1pbiIsImNyZWF0ZS1yZWFsbSIsIkFyZWFNYW5hZ2VyIiwib2ZmbGluZV9hY2Nlc3MiLCJSb290QWRtaW4iXSwicHJlZmVycmVkX3VzZXJuYW1lIjoiYmFkaWkiLCJlbWFpbCI6ImNsYXVkaW8uYmFkaWlAdW5pZmkuaXQifQ.QVUdR6D5YjnhPPPSkxkvHmBvqxGaRSwxyBGfse0mUqNDfZ4ffo6Mzl0air7qPiPMf4ZI_kryJ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R1wm9qCNISR5fFOGsXvCgxHvG4O_1qXbMSUvRt4JE4-RMZtscI5k2xOZAI8Q",
"token_type": "bearer",
"not-before-policy": 1554127150,
"session_state": "09506487-bf74-4736-b958-f594b9041916"
}
 

In R STUDIO we have:

 
# Get the access token 
url <- "https://www.snap4city.org/auth/realms/master/protocol/openid-connect/token/"
params <- list(client_id="rstudio-pax", grant_type="password", username="..............", password="....................")

post_req <- POST(url, body = params, encode = "form", verbose())

print(post_req)

content(post_req)

# Get the My Kpi data
url <- "https://www.snap4city.org/mypersonaldata/api/v1/kpidata/........kpiID......./values?sourceRequest="..........string for log with your project name......."
access_token <- content(post_req)$access_token
header <- paste("Bearer", access_token)
get_req <- GET(url,  accept_json(),  add_headers('Authorization' = header))
content(get_req, type="application/json")