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 | | 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.
// 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
--
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/-/2tlMO7J-f-cJ.
ThanksGunjan Sharma--
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/-/LT1TEHDujR0J.
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.
ThanksGunjan Sharma--
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/-/LT1TEHDujR0J.
ThanksGunjan Sharma--
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/-/LT1TEHDujR0J.
ThanksGunjan Sharma
--
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/-/TIKSjpI_OpkJ.