0

I'm using Google Apps Script (Javascript) to retrieve the details of a user using the Users.get method in the Google Apps Admin SDK Directory API.

The code to get the user is:

var url = 'https://www.googleapis.com/admin/directory/v1/users/'+email+'?key='+publicApiAccessKey;
var scope = "https://www.googleapis.com/auth/admin.directory.user.readonly";

var fetchArgs = googleOAuth_("Users",scope);
fetchArgs.method = "GET";
fetchArgs.muteHttpExceptions=true;

var userObject = UrlFetchApp.fetch(url, fetchArgs);

The response is a Google Apps Admin SDK User resource and object in this format:

{
  "kind": "admin#directory#user",
  "id": string,
  "etag": etag,
  "primaryEmail": string,
  "name": {
    "givenName": string,
    "familyName": string,
    "fullName": string
  },
  "isAdmin": boolean,
  "isDelegatedAdmin": boolean,
  "lastLoginTime": datetime,
  // rest of the properties excluded
}

How do I get the value of fullName?

Here's what I tried so far, with no success:

Logger.log( Object.prototype.toString.call(userObject) ); // returns [object JavaObject]
Logger.log( userObject.name.fullName ); // Cannot read property "fullName" from undefined.
Logger.log( userObject.fullName ); // undefined
Logger.log( userObject.name ); // undefined
Logger.log( userObject['fullName'] ); // undefined
Logger.log( userObject['name'] ); // undefined
Logger.log( userObject['name','fullName'] ); // undefined
5
  • 1
    userObject is clearly not the object described. If it were, then your userObject.name.fullName would be correct. I would suggest Logger.log(userObject); and/or for (var name in userObject) { Logger.log(name); } and/or Object.prototype.toString.call(userObject); to figure out what it is. For instance, could it be an array? typeof anArray returns "object". I can't see why it would return an array, but... Commented Jul 27, 2014 at 21:12
  • 1
    Hmm, ok. Object.prototype.toString.call(userObject) returns [object JavaObject] and for (var name in userObject) { Logger.log(name); } seems to just list the methods of the object (same as the autocomplete code suggestions I get when you type userObject followed by a dot: [14-07-27 17:21:45:843 EDT] getResponseCode [14-07-27 17:21:45:843 EDT] getContentText [14-07-27 17:21:45:844 EDT] getContent [14-07-27 17:21:45:844 EDT] getAs [14-07-27 17:21:45:844 EDT] getHeaders [14-07-27 17:21:45:844 EDT] getBlob [14-07-27 17:21:45:845 EDT] toString [14-07-27 17:21:45:845 EDT] getAllHeaders Commented Jul 27, 2014 at 21:25
  • Side note: If it's been deserialized (and if typeof says anything other than "string" it has been), it's not a "JSON object" anymore. It's an object. JSON is a textual notation for data interchange. Commented Jul 27, 2014 at 21:25
  • Nope, nothing to do with Java, but not sure if it's related to GWT. I'm running this code in the Google Apps Script Script editor, which is basically sandboxed Javascript. What's confusing to me is that in the documentation for this get method, it just says If successful, this method returns an Users resource in the response body., and the page describing the resource says "The following JSON template is used for Users resources in the Directory API" but doesn't tell you how to retrieve values from it. Commented Jul 27, 2014 at 21:31
  • Found it: (developers.google.com/apps-script/reference/url-fetch/…). Let me do some reading on this. Commented Jul 27, 2014 at 21:44

2 Answers 2

1

Based on your comment to the question, I suspect you've done something like this:

var userObject = Users.get(/*...args...*/);

...in whatever layer you're using to get the response, and so userObject isn't the user object, it's some form of custom XHR-like object. The documentation you've linked to is specifically Java and Python, not browser-side. (Barring using a Java applet, of course.)

Check that documentation, most likely it expects you to provide a callback/lambda or call a method on that object (getContent looked tempting) to get the actual response.

Sign up to request clarification or add additional context in comments.

3 Comments

Updated my original question to include the API call which returns the object, for clarification. userObject.getContent() returns an array of integers (strange), perhaps they are string lengths? userObject.getContentText() returns the text of the object but in the [object String] type. I'm going to go see if I can find the Google Apps Script documentation for these types of objects.
Figured it out: the HTTP GET request response contains data in JSON notation, but it needs to be parsed first, and Google Apps Script has a built-in JSON parsing library called JSON.parse which I learned about here. Thanks for pointing me in the right direction to help me realize it wasn't a valid Object type.
@mike: You're welcome! I suggest you post the correct way to process the call as an answer, and in a couple of days when SO lets you, accept your answer. The above got you started, but you got it across the finishing line, and from what you've described the above is incomplete (e.g., for others finding this in the future). :-)
1

As T.J. helped me discover, the Google Apps Script UrlFetchApp.fetch() method does not return a Javascript object. It returns what is basically a string that is structured in JSON notation, but it needs to be parsed first. There is a built-in library in Google Apps Script for Parsing JSON-structured strings.

Here is the final, working code:

var url = 'https://www.googleapis.com/admin/directory/v1/users/'+email+'?key='+publicApiAccessKey;
var scope = "https://www.googleapis.com/auth/admin.directory.user.readonly";

var fetchArgs = googleOAuth_("Users",scope);
fetchArgs.method = "GET";
fetchArgs.muteHttpExceptions=false;

var userJson = UrlFetchApp.fetch(url, fetchArgs);

var userObject = JSON.parse(userJson);

var fullName = userObject.name.fullName;

I learned about JSON.parse() here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.