I'm working with the Google Analytics API for the first time and I'm trying to create a new property. I wrote a JS function in Google App Script:
function insertProperty() {
var resource =
{
// "accountId": "177832616",
"resource":{
"name": "Test Property 7",
// "dataRetentionResetOnNewActivity": false,
"websiteUrl": "https://www.test.com"
}
}
var accountID = '177832616';
var request = Analytics.Management.Webproperties.insert(resource, accountID);
// request.execute(function (response) { console.log(property.id) });
}
This is the error the API throws:
GoogleJsonResponseException: API call to analytics.management.webproperties.insert failed with error: Field name is required. (line 56, file "Code")
The insert() method seems to take two parameters: insert(Webproperty resource, string accountId);
Since it's not recognizing the name key/value I added to resource, my guess is I haven't declared the variable as a Webproperty type and I'm not sure how to do this. I assumed Webproperty was a { } variable type, but at this point I'm not sure what to try next. Doing research online, I'm not able to find anything regarding the API's Webproperty so any context/info is helpful.
From your question, I could understand that Google Analytics API is used with Advanced Google services of Google Apps Script. In this case, resource of Analytics.Management.Webproperties.insert(resource, accountId) can directly use the request body of the method of "Web Properties: insert". I think that the reason of your error is due to this. So please modify as follows and test it again.
From:
var resource =
{
// "accountId": "177832616",
"resource":{
"name": "Test Property 7",
// "dataRetentionResetOnNewActivity": false,
"websiteUrl": "https://www.test.com"
}
}
To:
var resource = {
"name": "Test Property 7",
"websiteUrl": "https://www.test.com"
}
Note:
When accountId is not correct, an error occurs. Please be careful this.
From iansedano's comment, in this case, request of var request = Analytics.Management.Webproperties.insert(resource, accountID); directly returns the values. So you can see the value like console.log(request) and console.log(request.toString()).
Reference:
Web Properties: insert
Related
I´m trying to update existing contact using People service from Google apps gs. I have a contact like this:
let contactInfo = {
"name" : "Jhon",
"lastName": "Doe",
"phone": "+1999999",
"rn": "people/c8289118840931811524",
"etag": "%EgkBAj0JPgs/Ny4aBAECBQciDDV1TzVFdXY0ckhnPQ=="
}
I created the folowing function in order to update contact more easily:
function updateContact(contactInfo){
//var updatePersonFields = "updatePersonFields=names,phoneNumbers,emailAddresses";
var bodyRequest = {
"resourceName": contactInfo.rn,
"etag": contactInfo.etag,
"names": [{
"givenName": contactInfo.name,
"familyName": contactInfo.lastName,
}],
"phoneNumbers": [{
'value': contactInfo.phone
}],
"emailAddresses": [{
'value': contactInfo.email
}]
};
People.People.updateContact(bodyRequest, contactInfo.rn);
}
However, the document guide for updateContact needs 3 parameters: Path parameters, Query Parameters and request body:
https://developers.google.com/people/api/rest/v1/people/updateContact
In order to update contact, I have to pass updatePersonfields, but it is a Query Parameter and the updateContact just only receive 2 parameters.
I know that etag and updatePersonfields is needed, as the link explain it:
https://developers.google.com/people/v1/contacts?hl=en#update_an_existing_contact
How can I add the Query Parameter updatePersonFields (in comments)?
If updatePersonField is not send it, I have the following error:
GoogleJsonResponseException: API call to people.people.updateContact failed with error:
updatePersonFields mask is required. Please specify one or more valid paths.
Valid paths are documented at
https://developers.google.com/people/api/rest/v1/people/updateContact.
Thanks you in advanced.
In your situation, please include updatePersonFields to 3rd argument of People.People.updateContact as an object.
In this case, when you use People API of Advanced Google services with the script editor of Google Apps Script, you can see the document of updateContact(resource: Peopleapi_v1.Peopleapi.V1.Schema.Person, resourceName: string, optionalArgs: Object) by the auto-completion of script editor.
So, when your script is modified, it becomes as follows.
From:
People.People.updateContact(bodyRequest, contactInfo.rn);
To:
People.People.updateContact(bodyRequest, contactInfo.rn, {updatePersonFields: "emailAddresses,names,phoneNumbers"});
Note:
When your bodyRequest is invalid value, an error occurs. Please be careful this.
Reference:
Method: people.updateContact
I am following grahamearley's guide in setting up my google sheets to read/write to my firestore database. I have created a service account assigned to be the project owner, generated the key and stored the relevant strings in my testFunction as follows:
function testFunction() {
var key = "-----BEGIN PRIVATE KEY-----\n<my private key>\n-----END PRIVATE KEY-----\n";
var email = "xxxx#<projectId>.iam.gserviceaccount.com";
var projectId = "<projectId>";
var firestore = FirestoreApp.getFirestore(email, key, projectId);
const data = {
"name": "test!!"
};
firestore.createDocument("FirstCollection", data);
}
However, when I try running this, I obtain the following error:
Error: Missing or insufficient permissions. (line 33, file "Util",
project "FirestoreApp")
I tried setting my database rule as follows, but the function still returned an error:
service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read;
allow write: if false;
}
}
}
I have searched here for questions with the same error of insufficient permissions, but have not found anything that helps me.
Could someone please advise me on finding out the issue and fixing it? Thank you.
Manifest:
{
"timeZone": "Europe/Paris",
"dependencies": {
"libraries": [{
"userSymbol": "FirestoreApp",
"libraryId": "1VUSl4b1r1eoNcRWotZM3e87ygkxvXltOgyDZhixqncz9lQ3MjfT1iKFw",
"version": "22"
}]
},
"exceptionLogging": "STACKDRIVER"
}
EDIT1: Added manifest file
EDIT2: Added db rules
I have since resolved this issue. It seems the problem was with my service account. I had selected Project>Owner instead of Datastore>Cloud Datastore Owner.
Thank you for all your help rendered.
I am trying to create an org unit in the Google Admin Console, but it seems to ask for two arguments and I cannot find what to put in the second, unless it is to add a user, and then I have no idea how to create the org unit. What I have is this:
function createOrg() {
var resource = {
name: "TEST",
description: "The test",
parentOrgUnitPath: "/",
blockInheritance: false
}
var org = AdminDirectory.Domains.insert(resource, ?????);
}
UPDATE
First I had to follow this link here to get the "Customer" which is really the ID of the admin user I suppose. Then another error came out which was "Missing required field: domainName", So I tried adding that, but again, another error which was "Invalid Value", so I still don't know what is wrong. Now the script is:
function createOrg() {
var resource = {
domainName: "TEST",
name: "TEST",
description: "The is a test",
parentOrgUnitPath: "/",
blockInheritance: false
}
var customer = 'USERID';
var org = AdminDirectory.Domains.insert(resource,customer);
}
UPDATE
As was mentioned below, the only thing that had to be changed was Orgunits instead of Domains:
var org = AdminDirectory.Orgunits.insert(resource,customer);
I am trying to learn how to use the Skyscanner Flights API with Google Script. It seems that the information available online is not adapted to newbies like me.
From what I got, the procedure to gain access to the flights' prices is :
- to send a HTTP POST request with information about which flights we want information about
- then send a HTTP GET request which will give us the pricing information
I would like to do that with Google Script.
Here is my code so far :
function sky1() {
/*
Link to Skyscanner.com help : http://business.skyscanner.net/portal/en- GB/Documentation/FlightsLivePricingList
Link to Skyscanner api demo (api key given there): http://business.skyscanner.net/portal/en- GB/Documentation/FlightsLivePricingQuickStart
*/
var apikey = "prtl6749387986743898559646983194";// is given on skyscanner website for testing
var url = "http://partners.api.skyscanner.net/apiservices/pricing/v1.0/?apikey=" + apikey;
// Post http request to skyscanner
var post_resp=sendHttpPost(url,apikey);
}
function sendHttpPost(url) {
// post_params
var post_params = {
"Country": "CH",
"Currency": "CHF",
"Locale": "en-GB",
"Adults": 1,
"Children": 0,
"Infants": 0,
"OriginPlace": "12015",
"DestinationPlace": "5772",
"OutboundDate": "2015-08-09",
"InboundDate": "2015-08-23",
"LocationSchema": "Default",
"CabinClass": "Economy",
"GroupPricing": true
};
var options =
{
"method" : "POST",
"contentType" : "application/json", // didn't get what this means
"payload" : JSON.stringify(post_params), // didn't get what this means
"muteHttpExceptions" : true, // avoids error message
};
var post_resp=UrlFetchApp.fetch(url,options);
Logger.log(post_resp.getResponseCode());
return post_resp;
}
Any help would be very appreciated. This gives me a 415 response rode instead of a 201 indicating that a session has been created.
PS: I am not a programmer, I would be very grateful if we keep thing simple.
Skyscanner API team here. You may be interested to see a reference Javascript implementation at https://github.com/Skyscanner/skyscanner-api-js. I also recommend using Fiddler (a network tracing tool) to compare the request/response from the test harness at http://business.skyscanner.net/portal/en-GB/Documentation/FlightsLivePricingQuickStart with that of your code.
Have been struggling with this..... Google Apps Script and the Big Query API are working well however when I try to use BigQuery.Tabledata.insertAll I keep getting an error saying 'no such field'.
When I try to run the same thing through the Google API explorer it works fine. The documentation says the command is :
BigQuery.TableData.insertAll(TableDataInsertAllRequest resource, String projectId, String datasetId, String tableId)
I have constructed the TableDataInsertAllRequest resource as per the documentation https://developers.google.com/bigquery/docs/reference/v2/tabledata/insertAll and it looks like this :
{
"kind": "bigquery#tableDataInsertAllRequest",
"rows":
[
{
"json":
{
"domain": "test",
"kind": "another test"
}
}
]
}
This matches my table schema.
When I run the command the error returned is :
{
"insertErrors": [
{
"index": 0,
"errors": [
{
"message": "no such field",
"reason": "invalid"
}
]
}
],
"kind": "bigquery#tableDataInsertAllResponse"
}
As I say the same TableDataInsertAllRequest resource works fine in the API explorer (clicking Try It on the documentation page above) it just does not work through Apps Script.
Any help gratefully received.
I've run into this too, and had somewhat better luck with this variation.
var rowObjects = [];
// Generally you'd do this next bit in a loop
var rowData = {};
rowData.domain = 'test';
rowData.kind = 'another test';
rowObjects.push(rowData);
// And at this point you'd have an array rowObjects with a bunch of objects
var response = BigQuery.Tabledata.insertAll({'rows': rowObjects}, projectId, datasetId, tableId);
Some things to note:
I don't indicate a kind -- it is implied by the call to insertAll()
I use dot notation (is that the right term?) rather than strings to stuff attributes into my "row objects"
I'm not sure which of these is the Secret Sauce. Anyways, in the end, the structure of the call looks about like this:
BigQuery.Tabledata.insertAll({'rows' : [
{
'domain' : 'test',
'kind' : 'another test'
}
]
},
projectId,
datasetId,
tableId);