0

I am running a R script on rstudio ide of posit workbench which hosted on Azure VM RHEL 8 Linux server. I am trying to connect to azure application using Azureauth R package using Authorization-code method. When I run the script, I get local host refused to connect but the same Works for device-code method of the Azureauth R package.

The below is R code which I have tried to connect azure application.

R Script -

library(AzureAuth) 
redirect <- "http://localhost:1410" token <- AzureAuth::get_azure_token( "My_Resource", 
"My_Tenant_ID", 
"My_App_ID", 
use_cache = FALSE, 
auth_type = "authorization_code", authorize_args=list(redirect_uri=redirect) )

Error - localhost refused to connect**

But same code works via device-code method of azureauth r package

auth_type = "device_code"

I tried to open the new popped browser URL which gives out localhost refused to connect error via a Linux terminal command curl pop_browser_url and this is authenticating without any error .

Code with device code method works -

library(AzureAuth) redirect <- "http://localhost:1410" token <- AzureAuth::get_azure_token( "My_Resource", "My_Tenant_ID", "My_App_ID", use_cache = FALSE, auth_type = "device_code", authorize_args=list(redirect_uri=redirect) )

3
  • Can you share your device_code folw code in the question? Commented Oct 23, 2024 at 12:26
  • Check this Doc to fix the issue. Commented Oct 23, 2024 at 12:35
  • Can you this GitHub repo for Authorization_code flow. Commented Oct 24, 2024 at 5:03

1 Answer 1

0

I successfully retrieved the access token from Azure AD using Authorization_code flow with AzureAuth package in R.

I added the below URL in the Azure AD app's redirect URI under Web as shown below.

http://localhost:1410

enter image description here

Code :

library(AzureAuth)
library(httr)
library(jsonlite)

redirect <- "http://localhost:1410"    
resource <- "https://graph.microsoft.com"  
tenant_id <- "<tenantID>" 
app_id <- "<appID>" 

token <- AzureAuth::get_azure_token(
  resource = resource,
  tenant = tenant_id,
  app = app_id,
  use_cache = FALSE,  
  auth_type = "authorization_code",
  authorize_args = list(redirect_uri = redirect)
)

print(token)

cat("Access Token: ", token$credentials$access_token, "\n")

Output :

The following code ran successfully and redirect to the below page in browser to logged in to my Azure account.

enter image description here

I authenticated successfully after logged in as shown below.

enter image description here

I retrieved the access token using the authorization_code flow as shown below.

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your response. I had mentioned the same redirect url (i.e. localhost:1410) in my azure application and still I am getting the same error (local host refused to connect
If you still facing the same issue then, check this Docs to fix the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.