Google groups settings update issue

88 views
Skip to first unread message

robert dupont

unread,
Dec 5, 2011, 10:33:45 AM12/5/11
to google-app...@googlegroups.com
Hi,

I'm starting with this API and I'm trying to update a given group setting in Java.
I'm using the source code which is in google-api-services-groupssettings-v1-1.3.0-beta.jar.
Unfortunately I'm getting a 401 Unauthorized error when I run my app.
I'm maybe doing something wrong. I'm not able to solve this issue.
If someone has the answer it would be very helpful.


Thanks in advance.

Regards

Rob

You can find below the error message:

Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:138)
at com.google.api.client.googleapis.services.GoogleClient.execute(GoogleClient.java:123)
at com.google.api.client.http.json.JsonHttpRequest.executeUnparsed(JsonHttpRequest.java:67)
at com.google.api.services.groupssettings.Groupssettings$Groups$Update.execute(Groupssettings.java:614)
at com.ipsen.test.main(test.java:107)




You can find the java code below:

public static void main(String[] args) throws IOException {

String authorizeUrl = new GoogleAuthorizationRequestUrl(CLIENT_ID,
CALLBACK_URL, SCOPE).build();
System.out.println("Paste this url in your browser: " + authorizeUrl);

// Wait for the authorization code
System.out.println("Type the code you received here: ");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String authorizationCode = in.readLine();

// Exchange for an access and refresh token
GoogleAuthorizationCodeGrant authRequest = new GoogleAuthorizationCodeGrant(TRANSPORT,
JSON_FACTORY, CLIENT_ID, CLIENT_SECRET, authorizationCode, CALLBACK_URL);
authRequest.useBasicAuthorization = false;
AccessTokenResponse authResponse = authRequest.execute();
String accessToken = authResponse.accessToken;
GoogleAccessProtectedResource access = new GoogleAccessProtectedResource(accessToken,
TRANSPORT, JSON_FACTORY, CLIENT_ID, CLIENT_SECRET, authResponse.refreshToken);

HttpRequestFactory rf = TRANSPORT.createRequestFactory(access);
System.out.println("Access token: " + authResponse.accessToken);

// Make an authenticated request
Groups groups = new Groups();
groups.setWhoCanPostMessage("ANYONE_CAN_POST");

Groupssettings settings = new Groupssettings(TRANSPORT,JSON_FACTORY);
settings.builder(TRANSPORT, JSON_FACTORY);
Groupssettings.Groups.Update update = settings.groups().update(groupemail,groups);
groups = update.execute();

}



Gunjan Sharma

unread,
Dec 5, 2011, 11:07:46 AM12/5/11
to google-app...@googlegroups.com
Hello Robert

Have you requested access to the Groups Settings API? The API is still in experimental phase so you have to explicitly ask for access to it. You can do so by going to the Google API console. Go to services tab and then request access for the Groups Settings API.

Thanks
Gunjan Sharma |  Developer Programs Engineer | gunjan...@google.com |  +91 7702534446




--
You received this message because you are subscribed to the Google Groups "Google Apps Domain Information and Management APIs" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-apps-mgmt-apis/-/U5nJ3LuHbXkJ.
To post to this group, send email to google-app...@googlegroups.com.
To unsubscribe from this group, send email to google-apps-mgmt...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-apps-mgmt-apis?hl=en.

robert dupont

unread,
Dec 5, 2011, 12:22:26 PM12/5/11
to google-app...@googlegroups.com
Hi Gunjan,

Yes we have access to those APIs

Regards,

Rob

2011/12/5 Gunjan Sharma <gunjan...@google.com>

Gunjan Sharma

unread,
Dec 5, 2011, 5:08:11 PM12/5/11
to google-app...@googlegroups.com
Hello Robert

Authenticating to Groups Settings API is a little tricky. Here is a sample code doing the updation.


    // Get the Authorization URL

    String authorizeUrl = new GoogleAuthorizationRequestUrl(CLIENT_ID, CALLBACK_URL, SCOPE).build();

    System.out.println("Paste this url in your browser:\n" + authorizeUrl);

    // Wait for the authorization code

    System.out.println("Type the code you received here: ");

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

    String authorizationCode = in.readLine();

    // Exchange for an access and refresh token

    AccessTokenResponse authResponse = new GoogleAuthorizationCodeGrant(

        TRANSPORT, JSON_FACTORY, CLIENT_ID, CLIENT_SECRET, authorizationCode,

        CALLBACK_URL).execute();

    System.out.println("Access token: " + authResponse.accessToken);    

    GoogleAccessProtectedResource accessProtectedResource =

        new GoogleAccessProtectedResource(

            authResponse.accessToken, TRANSPORT, JSON_FACTORY, CLIENT_ID,

            CLIENT_SECRET, authResponse.refreshToken);

    // Make an authenticated request

    Groupssettings settings = Groupssettings.builder(TRANSPORT, JSON_FACTORY)

        .setApplicationName("GoupsSettingsJavaSample")

        .setHttpRequestInitializer(accessProtectedResource)

        .build();

    Get getRequest = settings.groups().get("Your Group");

    getRequest.set("alt", "json");

    Groups group = getRequest.execute();

    System.out.println(group.getName());

    

    group.setWhoCanPostMessage("ANYONE_CAN_POST");

    Update updateRequest = settings.groups().update("Your Group", group);

    updateRequest.set("alt", "json");

    group = updateRequest.execute();

There are few things to note here . You have to use Groupssettings builder function to build the client. You need to assign the accessProtectedResource to the client as it contains your access token. Other than that for update you cannot set a parameter in empty object and call update. You need to modify a retrieved Groups object update the required fields and call update. There are other ways to update where you don't can do it without retrieving the group first. Refer this. I hope it helps.

Let us know if there is any other issue.


Thanks

Gunjan Sharma

robert dupont

unread,
Dec 5, 2011, 5:31:24 PM12/5/11
to google-app...@googlegroups.com
Gunjan,

Thanks a lot for your reactivity and your help.
I will test this sample tomorrow and I will let you know if everythings is ok.
You are right, it's a little tricky compare to the others APIs.
Thanks a lot again.


Regards,


Rob

2011/12/5 Gunjan Sharma <gunjan...@google.com>
Hello Robert

--
You received this message because you are subscribed to the Google Groups "Google Apps Domain Information and Management APIs" group.

robert dupont

unread,
Dec 7, 2011, 7:56:05 PM12/7/11
to google-app...@googlegroups.com
Hi Gunjan,

I just want to tell you a huge thanks.
Your sample works great.
I also tested the API with others settings, and I can update the settings.

Just a question: Is it possible to use this API with a client Login authentication (not Oauth)?


Thanks a lot for your help!

Rob

2011/12/5 robert dupont <robert....@gmail.com>

Gunjan Sharma

unread,
Dec 8, 2011, 10:41:33 AM12/8/11
to google-app...@googlegroups.com
Hello Robert

Google do not encourages use of ClientLogin as they require you send your credentials over wire. So all the new APIs do not support ClientLogin. And I would suggest you to use OAuth for all APIs.

Thanks
Gunjan Sharma

Sunny Thakur

unread,
Dec 9, 2011, 12:54:01 AM12/9/11
to google-app...@googlegroups.com
Hi Gunjan ,
                 I have one query related to compose event in chrome extension .I want to alert in my extension when the user click on compose button in gmail. Like in background.html i want to detect the compose event ,if any user click on compose button than an alert should be there like "hi i am in compose "



Thanks
Gunjan Sharma

--
You received this message because you are subscribed to the Google Groups "Google Apps Domain Information and Management APIs" group.

To post to this group, send email to google-app...@googlegroups.com.
To unsubscribe from this group, send email to google-apps-mgmt...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-apps-mgmt-apis?hl=en.



--
Thanks and Regards,
Sunny

robert dupont

unread,
Dec 9, 2011, 4:40:53 AM12/9/11
to google-app...@googlegroups.com
Hi Gunjan,

Ok thanks for the reply

Regards,
Rob

2011/12/8 Gunjan Sharma <gunjan...@google.com>

Thanks
Gunjan Sharma

--
You received this message because you are subscribed to the Google Groups "Google Apps Domain Information and Management APIs" group.

robert dupont

unread,
Dec 9, 2011, 12:32:21 PM12/9/11
to google-app...@googlegroups.com
Hi Gunjan,

So if I understand right the google groups settings API just works with the Oauth.
But if I want  my application to be launched automatically with no human interaction(cron jobs) how can I use the Oauth authentication because in your sample code we have to retrieve and paste an authorization code and I'm wondering if it's possible to make this automatic.

Thanks you in advance

Regards,

Rob


2011/12/8 Gunjan Sharma <gunjan...@google.com>
Hello Robert

Thanks
Gunjan Sharma

--
You received this message because you are subscribed to the Google Groups "Google Apps Domain Information and Management APIs" group.

Gunjan Sharma

unread,
Dec 9, 2011, 1:22:37 PM12/9/11
to google-app...@googlegroups.com
Hello Robert

You don't need to get authorized all the time. Once you are authorized can store the access token and refresh it every time you get an Unauthorized exception. The refreshing can be done using accessProtectedResource.executeRefreshToken().

Other than that you can also create a authorization flow instead of manually pasting the code. In this case the code will be sent to your callback url and your application can extract it from the request. This means your application need to be a web application.

Thanks
Gunjan Sharma

robert dupont

unread,
Dec 15, 2011, 4:42:59 PM12/15/11
to google-app...@googlegroups.com
Hi Gunjan,

Thanks to your advices it's the update works but unfortunately when I try my app in my company environment it doesn't work because there is a proxy.
I would like to know if a way exists to provide the credentials to the proxy in order to use it?
(I already used proxy server with some google data API client library, but I'm not able to do it with groups settings APi and the oauth authentication)

Thanks in advance,

Robert

2011/12/9 Gunjan Sharma <gunjan...@google.com>

Thanks
Gunjan Sharma

--
You received this message because you are subscribed to the Google Groups "Google Apps Domain Information and Management APIs" group.

Gunjan Sharma

unread,
Dec 16, 2011, 6:17:00 AM12/16/11
to google-app...@googlegroups.com
Hello Robert

The API sees to work fine. I am sure there should be some way to authenticate even in case of proxy server. Please post your question at Java Client library support.

Thanks 
Gunjan Sharma
Reply all
Reply to author
Forward
0 new messages