2

In my node (v5.5.0) app, I'm making a request to the Google Groups Settings API via the google-oauth-jwt package but the response is coming back as XML (or ATOM?). The documentations seems to suggest you can your response as either ATOM or JSON, but it's not clear how. I'm also wondering why google-oath-jwt doesn't do the parsing.

My Code:

require('dotenv').load({ silent: true });
var request = require('google-oauth-jwt').requestWithJWT();

const email = process.env.GOOG_SERVICE_ACCOUNT;
const key = new Buffer(process.env.GOOG_SERVICE_ACCOUNT_KEY_BASE64, 'base64').toString();

const GoogleApiRequester = {

  get: function(url, scopes, delegationEmail, callback) {

    const jwt = { email, key, scopes, delegationEmail };

    const options = {
      method: 'get',
      url,
      jwt,
      json: true
    };

    request(options, (err, res, body) => {
      if (err) return callback(err);
      if (body.error) return callback(body.error);
      return callback(null, body);
    });
  }
}

The response:

<?xml version="1.0" encoding="UTF-8"?>\n<entry xmlns="http://www.w3.org/2005/Atom" xmlns:apps="http://schemas.google.com/apps/2006" xmlns:gd="http://schemas.google.com/g/2005">
 <id>tag:googleapis.com,2010:apps:groupssettings:GROUP:testing123@mydomain.org</id>
 <title>Groups Resource Entry</title>
 <content type="text">Administrators</content>
 <author>
  <name>Google</name>
 </author>
 <apps:email>testing123@mydomain.org</apps:email>
 <apps:name>Administrators</apps:name>
 <apps:description>All admin at 13W320</apps:description>
 <apps:whoCanJoin>CAN_REQUEST_TO_JOIN</apps:whoCanJoin>
 <apps:whoCanViewMembership>ALL_MANAGERS_CAN_VIEW</apps:whoCanViewMembership>
 <apps:whoCanViewGroup>ALL_MEMBERS_CAN_VIEW</apps:whoCanViewGroup>
 <apps:whoCanInvite>ALL_MANAGERS_CAN_INVITE</apps:whoCanInvite>
 <apps:whoCanAdd>ALL_MANAGERS_CAN_ADD</apps:whoCanAdd>
 <apps:allowExternalMembers>false</apps:allowExternalMembers>
 <apps:whoCanPostMessage>ANYONE_CAN_POST</apps:whoCanPostMessage>
 <apps:allowWebPosting>true</apps:allowWebPosting>
 <apps:maxMessageBytes>26214400</apps:maxMessageBytes>
 <apps:isArchived>false</apps:isArchived>
 <apps:archiveOnly>false</apps:archiveOnly>
 <apps:messageModerationLevel>MODERATE_NONE</apps:messageModerationLevel>
 <apps:spamModerationLevel>MODERATE</apps:spamModerationLevel>
 <apps:replyTo>REPLY_TO_IGNORE</apps:replyTo>
 <apps:customReplyTo/>
 <apps:sendMessageDenyNotification>false</apps:sendMessageDenyNotification>
 <apps:defaultMessageDenyNotificationText/>
 <apps:showInGroupDirectory>false</apps:showInGroupDirectory>
 <apps:allowGoogleCommunication>false</apps:allowGoogleCommunication>
 <apps:membersCanPostAsTheGroup>false</apps:membersCanPostAsTheGroup>
 <apps:messageDisplayFont>DEFAULT_FONT</apps:messageDisplayFont>
 <apps:includeInGlobalAddressList>true</apps:includeInGlobalAddressList>
 <apps:whoCanLeaveGroup>ALL_MEMBERS_CAN_LEAVE</apps:whoCanLeaveGroup>
 <apps:whoCanContactOwner>ANYONE_CAN_CONTACT</apps:whoCanContactOwner>
</entry>
Jared_C
  • 649
  • 1
  • 7
  • 16
  • 1
    I've been googling for a while but haven't found a definitive answer for you. I found this [thread](https://github.com/google/google-api-php-client/issues/559) in github though. It mentions something about setting the value of `alt` to `"json"`, which I saw in a sample [here](https://developers.google.com/admin-sdk/groups-settings/quickstart/nodejs#step_3_set_up_the_sample). Care to check it out. Let me know if it is useful. Good luck. – AL. May 04 '16 at 08:05
  • I updated my options var with alt: 'json' but it still comes back as xml. – Jared_C May 05 '16 at 21:03
  • Odd. If you don't mind, can you post the generated URL that returns this response? – AL. May 06 '16 at 00:42
  • What's more odd is, as per the [docs](https://developers.google.com/admin-sdk/groups-settings/firstapp#learn-about-the-json-and-atom-data-formats), the default return format is json. You can also see in that doc that setting value of `alt` determines the response format. I'm quite interested as to what's causing this. Let me know if you have any updates. – AL. May 06 '16 at 00:48
  • URL is https://www.googleapis.com/admin/directory/v1/groups/testing123@mydomain.org – Jared_C May 08 '16 at 17:44
  • This is a weird behavior. The generated url doesn't specify an `alt` value, hence it should default to JSON. I'll look around and let you know if I find anything. :) – AL. May 09 '16 at 10:23
  • Do tell if you also found a solution, or what was causing the response to return in atom format. – AL. May 09 '16 at 11:08

1 Answers1

1

I use the ServiceAccount credentials.

function getGoogleGroupSettings(auth) 
{
   const service2 = google.groupssettings({version: 'v1', auth});
   service2.groups.get({
        alt:"json", //without this parameter, Atom+XML format is returned
        groupUniqueId: 'testing@example.domain.com' 
      }, (err, res) => {

    if (err) return console.error('The API returned an error:', err.message);

    console.log(res);
  });
}

Use the following as a template for a complete example.

https://developers.google.com/admin-sdk/directory/v1/quickstart/nodejs#step_3_set_up_the_sample

Here is a C# example in SO

Unable to retrieve settings for Google Group using .NET client library

user3526
  • 1,412
  • 1
  • 19
  • 26