Execution API Google Apps Script Permissions - java

Whenever my spreadsheet is edited I want my separate Java code to be notified.
Google Apps Script has Simple Triggers: https://developers.google.com/apps-script/guides/triggers/
I tried out the trigger for onEdit and tested it out by making it that whenever my spreadsheet was edited I'd get an email.
This worked.
Now I'd like to connect the script to my Java code: https://developers.google.com/apps-script/guides/rest/quickstart/java
I went through the quickstart with the sample appscript and it worked. The java file printed out the contents of the root folders of Google Drive.
When I tried to apply the same steps to my onEdit script I get an Permissions Denied error in terminal.
I'm wondering if it's because the simple trigger is bound to the spreadsheet and the Execution API is for unbounded scripts?
How can I connect the onEdit function to Java code?
Here's my error message:
com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "The caller does not have permission",
"reason" : "forbidden"
} ],
"message" : "The caller does not have permission",
"status" : "PERMISSION_DENIED"
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1065)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
at Quickstart.main(Quickstart.java:179)

Related

Firebase FCM returning 403 SenderId mismatch PERMISSION_DENIED while trying to push a notification

I'm trying to setup a dev environment for developing with FCM and I'm getting this error.
I'm using the configuration of out-of-box and it is been loaded properly (I've debugged).
I'm working with java 8 and spring 2.2.12 and firebase-admin 7.1.0.
com.google.api.client.http.HttpResponseException: 403 Forbidden
POST https://fcm.googleapis.com/v1/projects/projectt-82004/messages:send
{
"error": {
"code": 403,
"message": "SenderId mismatch",
"status": "PERMISSION_DENIED",
"details": [
{
"#type": "type.googleapis.com/google.firebase.fcm.v1.FcmError",
"errorCode": "SENDER_ID_MISMATCH"
}
]
}
}
What I've tried so far
Create a new project and a new key
There was an old documented error where the services were not activated by default. I've checked the services and all of them are active.
Also, the user appears to have enough privileges.
I've also updated the library to the latest version (it also happened with the previous version)
Am I missing something here?
UPDATE
Same code, later on the following day, without any changes to the code, it started working. So the conclusion was that there was some problem with the service itself.

Why is Google Cloud API trying to connect as an end-user?

I am trying to use the Google Cloud Translate API. I generated a JSON file from a service account and set the GOOGLE_APPLICATION_CREDENTIALS to where the JSON file is saved. I then used it in a program like so:
import com.google.cloud.translate.*;
...
Translate translate = TranslateOptions.getDefaultInstance().getService();
Translation translation = translate.translate(message);
But I get the following error
com.google.cloud.translate.TranslateException: Your application has authenticated using end user credentials from the Google Cloud SDK or Google Cloud Shell which are not supported by the translate.googleapis.com. We recommend that most server applications use service accounts instead. For more information about service accounts and how to use them in your application, see https://cloud.google.com/docs/authentication/
at com.google.cloud.translate.spi.v2.HttpTranslateRpc.translate(HttpTranslateRpc.java:61)
at com.google.cloud.translate.spi.v2.HttpTranslateRpc.translate(HttpTranslateRpc.java:144)
at com.google.cloud.translate.TranslateImpl$4.call(TranslateImpl.java:113)
at com.google.cloud.translate.TranslateImpl$4.call(TranslateImpl.java:110)
at com.google.api.gax.retrying.DirectRetryingExecutor.submit(DirectRetryingExecutor.java:89)
at com.google.cloud.RetryHelper.run(RetryHelper.java:74)
at com.google.cloud.RetryHelper.runWithRetries(RetryHelper.java:51)
at com.google.cloud.translate.TranslateImpl.translate(TranslateImpl.java:110)
at com.google.cloud.translate.TranslateImpl.translate(TranslateImpl.java:124)
at app.websockets.Messenger.onMessage(Messenger.java:31)
at org.java_websocket.server.WebSocketServer.onWebsocketMessage(WebSocketServer.java:569)
at org.java_websocket.drafts.Draft_6455.processFrame(Draft_6455.java:709)
at org.java_websocket.WebSocketImpl.decodeFrames(WebSocketImpl.java:367)
at org.java_websocket.WebSocketImpl.decode(WebSocketImpl.java:212)
at org.java_websocket.server.WebSocketServer$WebSocketWorker.run(WebSocketServer.java:925)
Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
"code" : 403,
"errors" : [ {
"domain" : "usageLimits",
"message" : "Your application has authenticated using end user credentials from the Google Cloud SDK or Google Cloud Shell which are not supported by the translate.googleapis.com. We recommend that most server applications use service accounts instead. For more information about service accounts and how to use them in your application, see https://cloud.google.com/docs/authentication/.",
"reason" : "rateLimitExceeded"
} ],
"message" : "Your application has authenticated using end user credentials from the Google Cloud SDK or Google Cloud Shell which are not supported by the translate.googleapis.com. We recommend that most server applications use service accounts instead. For more information about service accounts and how to use them in your application, see https://cloud.google.com/docs/authentication/.",
"status" : "PERMISSION_DENIED"
}
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1067)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
at com.google.cloud.translate.spi.v2.HttpTranslateRpc.translate(HttpTranslateRpc.java:130)
... 13 more
Here is my JSON file:
{
"type": "service_account",
"project_id": "ecstatic-motif-220300",
"private_key_id": "keyid",
"private_key": "somekey",
"client_email": "kokosole#ecstatic-motif-220300.iam.gserviceaccount.com",
"client_id": "100208235593900994013",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/kokosole%40ecstatic-motif-220300.iam.gserviceaccount.com"
}
Can anyone explain what is going on here? How can I fix it? Google Cloud SDK is not even installed.
You are authenticated as a user, while the API expects a service account, seen that you use Client Libraries with the "import com.google.cloud.translate.*;" statement. You may find related detail in the Setting up authentication paragraph on the "Translation API Client Libraries" page.

Drive API not running?

I'm trying to list of all files within Google Drive using Drive API. I've found one API which gives me this list.
When I try to run this API in API explorer it works fine. Give me list of all files
https://www.googleapis.com/drive/v3/files
But when I copy paste url in browser & runs it proving API key, Show me an error.
https://www.googleapis.com/drive/v3/files?key={YOUR_API_KEY}
{
"error": {
"errors": [
{
"domain": "global",
"reason": "insufficientFilePermissions",
"message": "The user does not have sufficient permissions for this file."
}
],
"code": 403,
"message": "The user does not have sufficient permissions for this file."
}
}
I'm pretty new in android. I just want retrieve file list. not search function.
The issue might be your file permission check this documentation for the file permissions -
https://developers.google.com/drive/v3/reference/permissions
Only OAuth 2.0 is supported for Google Drive API.
https://developers.google.com/drive/v3/web/about-auth

Spring cloud dataflow app http returns 401 unauthorized

I am testing scdf with iris PA example which deployed "http | pmml | log" example
But when I post a request to the server, I always got:
401 Unauthorized
like these:
{
"timestamp": 1484718462061,
"status": 401,
"error": "Unauthorized",
"message": "Full authentication is required to access this resource",
"path": "/"
}
When I added application.properties file with this configuration:
security.basic.enabled=false
to the folder same with the local server jar file, it seemed not working...
I still saw the default password been set in the console:
using default security password: ****-****....
Then I used the Basic Auth with username user and password ****-***
it gave:
{
"timestamp": 1484718712952,
"status": 401,
"error": "Unauthorized",
"message": "Bad credentials",
"path": "/"
}
Infos:
plateform: docker-toolbox
OS: debian:jessie
SCDF version: 1.1.1.RELEASE
This problem has been addressed in the recent 1.1.2.RELEASE of http-source application - more details on where to download the latest bits can be found in #1130.

Google Drive revokes my token

I am new to google drive and started with the tutorial quickstart.
After numerous attampts I run the program successfully.
However I had permission and authorisation problems when I was trying to delete files.
Drive service = getDriveService();
service.files().delete("FileId").execute();
So I decide to change the URL in the client_secret.json,
From: "auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs"
To: "auth_provider_x509_cert_url":"https://www.googleapis.com/auth/drive"
in the hope that I would now get the permission to delete files. However after executing the program again I didn't get the authorization tab of the internet browser like before.
As an example of the mentioned tab, here an image from google playground:
That's the reason why I delted the connection from my app to google drive website:
In addition i recreated also the client_secret.json file. However when I now execute the program I get only this error, independent what I am doing.
Exception in thread "main" com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{
"error" : "invalid_grant",
"error_description" : "Token has been revoked."
}
at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105)
at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)
at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)
at com.google.api.client.auth.oauth2.Credential.executeRefreshToken(Credential.java:570)
at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489)
at com.google.api.client.auth.oauth2.Credential.intercept(Credential.java:217)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:859)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
at main.java.DriveQuickstart.main(DriveQuickstart.java:101)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
So the two questions are what should i do to get the tutorial running again and how to set the permissions so that i can delete files?
Now I find out the problem:
I had to delete the directory "user.home/.credentials/drive-api-quickstart".
After that I can give the program the authorisation, which it needs.
Hope that someone will help this post ;)
I had a slightly different case. After getting the example in the Quick Start Guide to work a few months ago, I got the following error today when rerunning it.
Exception in thread "main" com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{
"error" : "invalid_grant",
"error_description" : "Bad Request"
}
Since the tokens were stored in TOKENS_DIRECTORY_PATH ("tokens"), the token stored there probably became invalid. Deleting directory tokens allowed me to once again get the browser page to select a gmail account to proceed with. Everything worked from there.

Categories

Resources