Error in Oauth2.0 authentication in R using HTTR (failed to connect: connection refused) - r

I am trying to create a simple app (in R) using GroupMe's API, which utilizes OAuth2.0. The documentation can be found here. However, I'm getting stuck on the first step of authentication/token generation for a user. See below for my code and the response I get:
access_key = ****
client_id = ****
gendpoint <- oauth_endpoint(
authorize = glue("https://oauth.groupme.com/oauth/login_dialog?client_id={client_id}"),
access = glue("http://localhost:1410/?access_token={access_key}")
)
gapp <- oauth_app(
"pingme",
key = client_id,
secret = access_key
)
t <- oauth2.0_token(
endpoint = gendpoint,
app = gapp
)
The above code is sufficient to bring in the login page, which presumably allows me to enter my credentials to obtain a token. However, when I enter my credentials, I get the following error message in R:
Authentication complete.
Error in curl::curl_fetch_memory(url, handle = handle) :
Failed to connect to localhost port 1410: Connection refused
So it looks like my authentication/login credentials worked, but somewhere an error prevented me from actually generating the token.
Could someone help me with this? This is my first time using the OAuth2.0 framework so I'm very confused. Thanks in advance!

Related

Getting different errors when try to run report for GA4 with regular account

I am trying to run a simple report on GA4 by using Google Analytics Data API Python client with a regular user credentials:
request = RunReportRequest(
property=f"properties/11111",
dimensions=[Dimension(name=f['name']) for f in report_definition['dimensions']],
metrics=[Metric(name=f['expression']) for f in report_definition['metrics']],
date_ranges=[DateRange(start_date=date, end_date=date)],
)
response = client.run_report(request)
And the client is BetaAnalyticsDataClient as also mentioned in the documentation:
credentials = Credentials(
token=None,
refresh_token=config['refresh_token'],
client_id=config['client_id'],
client_secret=config['client_secret'],
token_uri="https://accounts.google.com/o/oauth2/token",
scopes=["https://www.googleapis.com/auth/analytics.readonly"]
)
client = BetaAnalyticsDataClient(credentials=credentials)
It is not a Service Account so I am using google.oauth2.credentials.Credentials class as same in other Google APIs.
However, this operation is throwing an exception during the run_report function:
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
status = StatusCode.UNAVAILABLE
details = "Getting metadata from plugin failed with error: ('invalid_grant: Bad Request', {'error': 'invalid_grant', 'error_description': 'Bad Request'})"
debug_error_string = "UNKNOWN:Error received from peer analyticsdata.googleapis.com:443 {created_time:"2023-01-14T14:12:10.907813+03:00", grpc_status:14, grpc_message:"Getting metadata from plugin failed with error: (\'invalid_grant: Bad Request\', {\'error\': \'invalid_grant\', \'error_description\': \'Bad Request\'})"}"
>
And when I try to use my access token in the credentials:
credentials = Credentials(
token=config["token"],
refresh_token=config['refresh_token'],
client_id=config['client_id'],
client_secret=config['client_secret'],
token_uri="https://accounts.google.com/o/oauth2/token",
scopes=["https://www.googleapis.com/auth/analytics.readonly"]
)
This time I am getting following error:
google.api_core.exceptions.Unauthenticated: 401 Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.
I am sure that my credentials is correct since I am using same account in my other repos.
Also, note that, I tried same operation with a service account and it does not give any error. However, for this purpose, I need to use a regular developer account since the OAuth flow is on a frontend project.
What are the suggestions on that issue? Is it possible to use a developer account in here and if yes, how?
I was able to fix the issue. The app just needs a sign-out sign-in (or refreshing the access token).

Connecting to Azure Data Lake by R using SAS Token

I am trying to connect to Azure DataLake Gen2 via R. I get from Azure DataLake administrator read access to given folder.
I have:
blob_service = 'https://DataLakeName.blob.core.windows.net'
data_lake_storage = 'https://DataLakeName.dfs.core.windows.net'
pathToFolder = 'path/to/folder/with/acess'
blob_sas_token = 'blobksastoken1232133210'
blob_sas_url = 'blob_service/pathToFolder?blob_sas_token'
Accoring to information about library AzureStor I tried:
library(AzureStor)
end_point <- blob_endpoint(endpoint = "https://DataLakeName.blob.core.windows.net/", sas = blob_sas_token)
list_blob_containers(end_point)
Unfortunately I got HTTP status code 500, what means probably an internal server error. Administratior said that token is correct and endpoint also works fine.
Error in process_storage_response(response,
match.arg(http_status_handler), : Internal Server Error (HTTP
500). Failed to complete Storage Services operation. Message:
InternalError Server encountered an internal error. Please try again
after some time.
Any idea what I am doing wrong, or how can I connect to ADL in other way using data that I have?

How Do I Connect to a Neo4J AuraDB Instance from R Studio

I created a Neo4j AuraDB database where I have dumped the movie recommendation dataset. I am able to start and connect to the instance in the cloud. However, when I try to connect to the instance via its API in R Studio, using the neo4r package and the following code
movieDB <- neo4j_api$new(
url = curl("neo4j+s://0cc45a14.production-orch-0054.neo4j.io:7687"),
user = "neo4j",
password = "password"
)
movieDB$ping()
I get the error message in my console Error: Could not resolve host: 7. Also, when I try to start a graph with the following code
graph <- startGraph(
url = curl("neo4j+s://0cc45a14.production-orch-0054.neo4j.io:7687"),
user = "neo4j",
password = "password"
)
I also get the following error message in my console
Error in function (type, msg, asError = TRUE) :
Could not resolve host: 8
I do not know why the error is happening, as I have previously connected to a Neo4j sandbox instance from within R Studio without any hassle. As always, I will appreciate your helpful suggestions. Thanks!
AuraDB requires a driver which supports the Bolt protocol. I do not know of any R driver that supports the protocol at the moment, they only support HTTP (and HTTP is not available yet in AuraDB).

OAuth2 authentication with R using httr

I am trying to read some data from the Polar Accesslink API. I have read the documentation and examples (I'm not that familiar with Python) as well as httr examples/demos of Oauth2.0:
https://www.polar.com/accesslink-api/#polar-accesslink-api
https://github.com/polarofficial/accesslink-example-python
Here is my code:
library(httr)
client_id <- rstudioapi::askForPassword()
client_secret <- rstudioapi::askForPassword()
# redirect_uri determined in registration to http://localhost:1410/
app <- oauth_app(appname = "polar_app",
key = client_id,
secret = client_secret)
endpoint <- oauth_endpoint(
request = NULL,
authorize = "https://flow.polar.com/oauth2/authorization",
access = "https://polarremote.com/v2/oauth2/token")
token <- oauth2.0_token(endpoint = endpoint,
app = app,
scope = "accesslink.read_all", # tested also without scope
use_basic_auth = T # tested both T and F
)
Last part of the code (token) produces the following error:
Waiting for authentication in browser...
Press Esc/Ctrl + C to abort
Authentication complete.
Error in oauth2.0_access_token(endpoint, app, code = code, user_params = user_params, :
Bad Request (HTTP 400). Failed to get an access token.
It opens browser with url localhost:1410/?state={STATE}&code={CODE} and says:
"Authentication complete. Please close this page and return to R."
Based on the API documentation and github Python examples, I can't figure out if I need to set some values for user_params or query_authorize_extra in oauth2.0_token.
Having the same issue. Sent a message off to support and they came back with making sure that you include header "Content-Type: application/x-www-form-urlencoded" and that the body gets sent as plain/raw text and contains "grant_type=authorization_code&code=<AUTHORIZATION_CODE>".
I have not tested this out but hoping it helps you out if you have not already found a solution

Run Batch Process with Oauth2.0_token

I am trying to connect to the Instagram API using R and want my code to run as a batch process on an Ubuntu server. Currently, I am using oauth2.0_token and running into an error when it needs to connect to the browser for authentication.
When I try to connect to the API on the server, I get this message
Waiting for authentication in browser...
Press Esc/Ctrl + C to abort
What would be a way to get around this? Or is there a better way to connect to the Instagram API?
Here is an example of the code I am currently using that is producing the error
app_name <- 'Testing'
client_id <- 'XXXXXXX'
client_secret <- 'XXXXXXX'
scope = 'basic'
instagram <- oauth_endpoint(
authorize = "https://api.instagram.com/oauth/authorize",
access = "https://api.instagram.com/oauth/access_token")
myapp <- oauth_app(app_name, client_id, client_secret)
ig_oauth <- ?oauth2.0_token(instagram, myapp,scope="basic",
type = "application/x-www-form-urlencoded",cache=FALSE)

Resources