Google sheets & google analytics authentication - google-apps-script

I'm trying to use the real-time-data export for google analytics in google sheets scripts.
However my google analytics api/ developer console is under a different account as my google sheets account.
I specify the project my google sheets script should use in the following line:
var ana = Analytics.Data.Realtime.get('ga:xxxxxxxxx','rt:activeUsers, rt:source', {'dimensions' : 'rt:source,rt:medium'});
However this throws the error:
Access Not Configured. Google Analytics API has not been used in project 1046300490979 before or it is disabled.
When i open up this error and click the link provided it sends me to a different project then the project id i specify in the line of code above.
How would i go about getting google analytics data through the API of a different account?
P.S.: I have followed the following guide to set up my google sheets script: https://www.tatvic.com/blog/google-analytics-real-time-data-in-google-sheet/
EDIT: I found out that you can change the project connected to your script.
However after doing so i am getting the following error:
User does not have sufficient permissions for this profile. (regel 3, bestand 'Code')
In the developer console i have access to everything.

Turns out i was using the wrong id's so i'll elaborate a little on where to get what id's in the tutorial above.
Under Resources -> Cloud Platform-project you have to link your script to your google developer console.
You can find this id by going to your developer console, selecting the project connected to your google analytics account. There should be a projectID under Projectinformation
In the following line:
var ana = Analytics.Data.Realtime.get('ga:xxxxxxx', 'rt:activeUsers', {'dimensions' : 'rt:source,rt:medium'});
xxxxxxx should be replaced by your google analytics project id, open your google analytics and look at the URL it should be all numbers behind the p.
https://analytics.google.com/analytics/web/#/report-home/a75228945w113577272p128654983

Related

How do you access the Google Sheets API via a Google Apps Script?

I am trying to call the Google Sheets API via a Google Apps Script. As an example, I have followed the instructions provided here:
https://developers.google.com/sheets/api/quickstart/js
I have generated the client ID and API Key via my Google Cloud Platform project as suggested elsewhere.
Now, I have created a google apps script with two files, the Code.gs file and a file called index.html containing the code copied and pasted from the Google Sheets API Quickstart above (with my client ID and API inserted).
When testing deployment, I get the following error message:
{
"error": "idpiframe_initialization_failed",
"details": "Not a valid origin for the client: https://<some numbers>-script.googleusercontent.com has not been registered for client ID <MY_CLIENT_ID>. Please go to https://console.developers.google.com/ and register this origin for your project's client ID."
}
I have registered script.google.com as an origin for my project.
Am I doing something obviously wrong? How would I proceed from here to get past this error?
If you want to make use of the Sheets API in Apps Script you will have to make use of the Sheets Advanced Service.
For this you will have to go to Services + and then add the Google Sheets API:
Since this is an advanced service, the authorization flow will be handled by Google so you won't have to provide the clientId and the API key.
Reference
Apps Script Advanced Google Services.

Get app_id from google sites

I want to grant access for my sites by the script, for google docs I can get the id from the url: https://docs.google.com/document/d/<doc_id>/edit
and do this: DriveApp.getFileById('<doc_id>').addViewer(emailid);
but for google sites I get this format instead:
https://sites.google.com/d/<id_1>/p/<id_2>/edit
I tried use id_1 and id_2 but both does not work.
Can anyone help me?
Thank you
The correct File Id to be used for Google Sites is the <id_1>.
The reason why you cannot add viewers to the site is because unlike Google Docs, Google Sites doesn't have a viewer sharing option. Instead, it uses a published viewer option.
Sample: 3 Users with different access rights
(I'm using new Google Sites in this example)
Sample Code:
function myFunction() {
Logger.log(DriveApp.getFileById('id_1').getViewers());
Logger.log(DriveApp.getFileById('id_1').getEditors());
}
Logs:
[21-01-07 00:52:50:404 HKT] []
[21-01-07 00:52:50:719 HKT] [DriveUser]
If you want to set viewers for your Google Site, you can use Sites Service in Apps Script. However, you can only use Site.addViewer(emailAddress) for classic sites.
Note:
A rebuilt version of Sites was launched on November 22, 2016. Apps Script cannot currently access or modify Sites made with this version, but script can still access classic Sites.
(Update)
Currently, there is no other option to update Google Sites via Apps Script or API.
Based on this reference: When will available the API Google Site?
The new version of Google Sites does not offer an API at the moment but Google has announced that API capabilities including Google Apps Script integration will be available.
For more detailed information see Deprecation Timeline - classic Google Sites
Google doesn't offer dates for when features will be available so you won't find one.

Unable to open Google xlsx spreadsheet / Also Google Drive permission Blocked

I have a Google Apps Script that I am currently using to successfully open a Google Sheets spreadsheet (read-only shared with my account) and perform some reading functions.
I received another spreadsheet that I would like to run similar processes on.
If I try:
SpreadsheetApp.openById("_______") (which works on the other sheet), I get Exception: Service Spreadsheets failed while accessing document with id _____" with this sheet. This spreadsheet is actually an xlsx on Google Drive (but appears it can still open in Google sheets) rather than a native Google sheet so I figured maybe that was the issue...?
So now I'm trying to instead access it as a Google Drive file using DriveApp.getFileById("____"). However now I get an authorization request to allow my script to access Google Drive. When I authorize it, I get:
This app is blocked
This app tried to access sensitive info in your Google Account. To keep your account safe, Google blocked this access.
I haven't found a way to fix this. I'm on a private Google account, not using a company's Apps.
Any ideas?
Found a solution to the permission issue.
To work with Apps Script, I've modified this from #alper's solution which sources #tellowkrinkle's comment.
Do the following:
Go to https://console.developers.google.com and create a new project for yourself
Search for the Google Drive API in the search box at the top, and turn it on
Click OAuth consent screen on the left and set it up.
Assuming your account isn't part of an organization, you'll have to say your app is for external users and in testing
In the required scopes section, add .../auth/docs and .../auth/drive (I'm not sure which needed, it's probably only one of those). This will probably not be available if you didn't complete (2)
Add the accounts you want to use with your copy of gdrive as testers of your app. Only these accounts will be able to use your copy of gdrive, so everyone will have to compile their own (unless someone goes and gets theirs reviewed by Google). I only added my own account because I am the only one using my App Script which uses my Drive.
Click the 3 dots on the top right and select "Project settings". Make note of the "Project number".
Go to your Apps Script. Go to Resources > Cloud Platform project. Insert the project number from (4).
Next time you try to run your Apps Script, you'll be asked to Authorize but this time you'll have the chance to actually do it successfully.

Force Google Apps users to change password periodically

I'm wondering if anyone has updated the following script for the new "Admin SDK Directory Service" as I currently get an error with the "UserManager" is not defined.
The original script is from this site (Thank you),
http://www.googleappsscript.org/home/force-google-apps-users-to-change-password-periodically
I'm not a developer but I have used this script before and it worked great.
This is the line that is giving you the error:
//get all users in domain
var users = UserManager.getAllUsers();
That API is now deprecated:
Google Documentation - google-apps/provisioning
In order to use the new API, it must be explicitly enabled in your Apps Script editor. And you will need a domain name.
Google Support - What is a domain?
If you have a domain name, you can use this example code, to get a list of all the users:
Apps Script Documentation - Admin SDK Directory Service
The code you were using would need to be totally re-written. It's not really what stackoverflow is meant for.

"Embed Google Docs Folders in a Google Site" script returns error. Is now required an additional permission to run it?

I added the "Embed Google Docs Folders in a Google Site" from de Google Spreadsheet script gallery and allowed to run, add the folder id and the URL of a Google Site page. It runs fine for files where I'm the owner or an editor but when I'm a viewer an error occurs and the following error message is displayed
You do not have permission to perform that action. (line 31)
The above occurred both with a Google Apps account and with a consumer account.
I'm wondering if an additional permission is required or if there is a another way to get the owner of docs shared with me.
Thanks in advance,
Rubén
Issue 1258: DocsList and View only Acccess was marked "WorkingAsIntended" August 22, 2012.
Following Google's sharing model, "Read Only" access does not give permissions to see document owners' names or ids.