'Request had insufficient authentication scopes' from Google Translate API in R - r

I am currently working on analyzing twitter data , however when I attempt to translate twitter text data through the translate() package leveraging the use of Google API in R I get this error.
t <- gl_translate(my_data$text, source="ar", target="en", )
2020-07-19 11:25:30 -- Translating text: 69597 characters -
ℹ
2020-07-19 11:25:32 > Request Status Code: 400
2020-07-19 11:25:32 -- API returned: Request payload size exceeds the limit: 204800 bytes.
2020-07-19 11:25:32 -- Attempting to split into several API calls
2020-07-19 11:25:32 -- Translating text: 98 characters -
Auto-refreshing stale OAuth token.
ℹ 2020-07-19 11:25:33 > Request Status Code: 403
Error: API returned: Request had insufficient authentication scopes.
why does it produce this error?
this is my code:
gar_auth(email="**#**.com")
set.key('****')
t <- gl_translate(my_data$text, source="ar", target="en", )
View(t)

If you use googleLanguageR package (it is not clear from the question itself), it seems like an authentication issue.
You'll probably need to authenticate with:
library(googleLanguageR)
gl_auth("translate_api_key.json")
To get the JSON authentication file, see Google Cloud Translation - Creating service accounts and keys.
For details on authenticating the googleLanguageR package in R, see Language tools for R via Google Machine Learning APIs - Authentication.
Note that Google API has some pricing for this.
If you want to use a Google Translate API key rather than JSON file, there are:
translate package, where you store you API key in getOption("google.key") variable, or you pass a key argument with you API key, and
github/sumtxt/datatools package with gl_translate() function, where you can provide the API key in the key argument as well,
but note that you have to generate you API key in Google Cloud Console as well (not to be confused with a password of your Google account).

Related

Is it possible to obtain an access_token with expo-auth-session/providers/google?

I am trying to obtain an access token for use with Google APIs using the up-to-date expo-auth-session package.
It works fine to fetch an id_token via useAuthRequest({responseType: "id_token", ...}) or useIdTokenAuthRequest({...})
However, when trying useAuthRequest({responseType: "code", ...}), I get the following error:
[Unhandled promise rejection: Error: The request is missing a required parameter, includes an unsupported parameter value (other than grant type), repeats a parameter, includes multiple credentials, utilizes more than one mechanism for authenticating the client, or is otherwise malformed.]
Does anyone know what is missing from the request? I am using the Expo Go app currently, so I'm not sure if that prevents obtaining an access token. Currently digging through the package's source code...
Add the following two props to useAuthRequest
shouldAutoExchangeCode: false,
clientSecret: 'any_bogus_value!'
shouldAutoExchangeCode will prevent the code from being exchanged and send it back to you in the response.
clientSecret will simply trigger and if check to be true in the providers source code that will avoid that error you specified being returned.
Note that in general it is not a good practice to avoid errors, but in this case, I feel like the error is being shown in error itself!

Microsoft Graph API - error 403 "Insufficient privileges to complete the operation"

I'm trying to use the AzureR family of R packages to interact with Outlook through the Graph API. Using Microsoft365R I have the following code:
outl <- get_business_outlook(
tenant = tenant_id,
app = client_id,
password = client_secret
)
But this results in a 403 error:
Error in process_response(res, match.arg(http_status_handler), simplify) :
Forbidden (HTTP 403). Failed to complete operation. Message:
Insufficient privileges to complete the operation.
The app in question has the API permissions Mail.ReadWrite, Mail.ReadWriteShared, Mail.Send, Mail.Send.Shared, offline_access, openid, User.Read.
I also tried using the AzureGraph package directly like:
login <- create_graph_login(
tenant = tenant_id,
app = client_id,
password = client_secret
)
This works and I get a token. I then try to extract user information with me <- login$get_user(), but this throws the same 403 error as above. I suspect there is something I need to do to actually authenticate the user, but I can't really figure out what.
I am entirely new to the Graph API so it's very possible that I have missed something obvious. Any help appreciated!
Microsoft365R/AzureGraph author here. In the code you show, both with get_business_onedrive() and create_graph_login(), you are authenticating as the app, not as the user. This means that there is no user account involved, hence you're unable to view user details or send email.
To authenticate as the user, run
# Microsoft365R
get_business_outlook("tenant_id", app="client_id")
# AzureGraph
create_graph_login("tenant_id", app="client_id")
ie, without the password argument. You should know it's working if R opens up a browser window for you to login to Azure (or to show it's successfully logged in).
The latest revision of the AzureAuth package has a vignette that explains a bit more on the various authentication scenarios. AzureAuth::get_azure_token is the underlying function used to obtain an OAuth token by Microsoft365R and AzureGraph, and you can pass down the arguments mentioned in the vignette from get_business_outlook and create_graph_login.

In R, with 'httr' package, where do I use 'provider ID' to connect to an API with Oauth2.0?

I'm trying to connect to an API with Oauth2.0 to obtain data using R. The database owner gave me the following credentials:
domain name: https://...
providerID: 7
client id: encoded string
client secret: encoded string
I'm trying to use the 'httr' package but I can't find providerID in any of the documentation and I'm new to Oauth2.0. I don't know which step in the dance the providerID is used. Getting the token initially, or making the query after?
Besides the httr documentation, my main reference is this, which also doesn't mention provider ID.
https://blog.r-hub.io/2021/01/25/oauth-2.0/
Thanks!
Credentials not documented in general API packages are probably API specific. Try setting them as a parameter in the URL query, or talk to the API creator.

How can export list of Cloud Messaging topics using Postman (or any client)?

I want to get a list of all Cloud Messaging topics that have been created by my app. Documentation shows that I need to fire a GET request to the https://iid.googleapis.com/iid/info/IID_TOKEN endpoint with an Authorization header containing my API key. For example:
https://iid.googleapis.com/iid/info/nKctODamlM4:CKrh_PC8kIb7O...clJONHoA?details=true
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
Steps:
Get IID_TOKEN by printing FIRInstanceID.instanceID().token() in my
iOS app. This returns a 152 character long string.
Get Authorization key from API_KEY in GoogleService-Info.plist. This is a 39 character long string.
Fire GET request using Postman Chrome Extension
Result:
I get MissingAuthorization. Either I am providing the wrong IID_TOKEN, API_KEY (unlikely), or setting authorization header incorrectly in Postman.
What am I doing wrong?
You can get the server key directly from Firebase -> Project settings -> Cloud Messagings:
After that, you can create a request using Postman in this way:
Or you can use Authorizations OAuth 2.0 in this way:

DocumentDB web API access with R

I have the following issue when trying to connect to the documentDB web API with R and PostMan.
In the DocumentDB documentation the way to ask something to the web API is to compose an Authorization header with base64 hash.
In R I'm trying to compute the signature and test the header directly with postman.
But I get every time a http 401.
Here is my R code:
toHash <- enc2utf8("get\ncolls\ndbs/toto/colls/testtoto\nsun, 08 may 2016 06:43:05 gmt\n\n")
hash <- hmac(key, toHash, "sha256")
base64(hash)
the "key" is the primary key got from the portal.
And then, following the Azure documentation, my header is:
type=master&ver=1.0&sig=< thebase64(hash) >
I'm pasting that into PostMan with the headers x-ms-version, date and x-ms-date.
But it'is not working..
I'm stuck now, does anyone have an idea? Am I using a wrong R function? A wrong key, is there a way to get more information about the mismatch?
The web api response is :
{
"code": "Unauthorized",
"message": "The input authorization token can't serve the request. Please check that the expected payload is built as per the protocol, and check the key being used. Server used the following payload to sign: 'get\ncolls\ndbs/toto/colls/testtoto\nsun, 08 may 2016 06:43:05 gmt\n\n'\r\nActivityId: fadbfc0b-e298-418a-b56c-8114699fff91"
}
I found what was wrong by myself.
The token given in the Azure portal is base64 encoded. So It is mandatory to decode it:
RCurl::base64Decode(key, mode="raw")
in order to use it with the digest::hmac function. It is also mandatory to specify raw = TRUE within this hmac function.

Resources