I'm trying to add a user to a Google Group using Google Apps Script.
Here's the code I've tried:
// Adds a user to a Google Group
function addUsertoGroup(userEmail) {
var userEmail = 'Name#gmail.com'
var groupId = "group-name#googlegroups.com";
var group = GroupsApp.getGroupByEmail(groupId);
// If email is already in group
try { var hasMember = group.hasUser(userEmail);}
catch(e){Logger.log(userEmail+" is already in the group"); return}
var newMember = {email: userEmail, role: "MEMBER"};
// This is the line which is throwing an error
AdminDirectory.Members.insert(newMember, groupId);
When running, I receive an error:
API call to directory.groups.get failed with error: Domain not found.
Any help would be appreciated.
Clarification:
You require a G Suite account to be able to use the AdminDirectory function (or to have the ability to add users to a group via Apps Script.
You can add gmail or any other non-same domain users based on the group setting you configure via https://groups.google.com
Solution:
I have a G Suite account via script.gs and I tested the code that you've shared - its perfect :) Except for the following that you need to enable, from a G Suite account.
Navigate to Resources > Advanced Google Services... and enable Admin Directory API
That's it. I created the group, enabled all settings, made the group accessible to everyone and it worked like a charm.
Let me know if you require any further clarification or assist as well.
Edit notes:
So, this is also what I had to follow to ensure everyone (even folks outside the domain could be added as users), as documented on Set Groups for Business sharing options. When you go to Groups for Business and navigate through the settings, you'd get to enable the following option that's critical -
Obviously, you're free to tweak all the other settings, as required.
You are trying to use the AdminDirectory service, this service will work only for G Suite Admin within a G Suite domain. So you need to have your domain and it will work if you add user from your domain to a group of your domain.
i.e. user#domain.com added to group#domain.com
Based on your sample code you try to add a gmail.com user to a public Google Groups for that you can't use the admin directory API because you are not admin of the domain that manage the public groups.
And in this case, as you did, you can only use the GroupsApp service and this service only allow you to check if user is in the ggroups or what is the role.
=> With GroupsApp service it is impossible to add a user to a public google group.
Stéphane
Related
I have several alias's for my Google Groups. The Google console search is really bad and won't return the group if just type of the email address. I wanted to use Google App Script to pass the email address and have it return the name of the group. Simple right. I have not been able to find an SDK that actually returns the Google Group Name. I have samples that tell me whos a member, I can list attributes about the group but nothing shows me the name of the actual group. Anyone have links or information about how I can get this.
I have already look at Googles API site before someone adds the link:
https://developers.google.com/apps-script/reference/groups/groups-app
https://developers.google.com/apps-script/reference/groups/group
https://developers.google.com/apps-script/advanced/admin-sdk-groups-settings
If anyone has a link or informatin on how to do this it would be greatly appreciated.
What you want can be achieved by using the AdminGroupsSettings advanced service.
You need to turn the Groups Settings API service on by going to your project's Resources > Advanced Google services...
Afterwards, look for the Groups Settings API in the list and activate it. As it can be seen on the third column, you can call the service by using AdminGroupsSettings.
Then, just use the below snippet
function getGroup() {
var groupName = AdminGroupsSettings.Groups.get('EMAIL_ADDRESS').name;
console.log(groupName);
}
Reference
Advanced Google Services Apps Script;
Directory API Groups:get.
Currently my code only modifies my signature, because when I put the email of the other person in my domain, the error: Not Found (line 9, file "Code") appears.
My current code:
function myFunction() {
var newSignature = Gmail.newSendAs();
newSignature.signature = "signature";
var listEmails = [
"leticia#domain.com"]
var updateSignature = Gmail.Users.Settings.SendAs.update(newSignature, "me", listEmails)
}
I am developing using APPS SCRIPT.
Any suggestions for me to be able to change the signature of someone else in my domain?
To change other people Gmail settings in your domain you'll need to be a domain Admin, then create a service account with domain-wide authority, then whitelist it in the Admin Console. Then use said service account and authentication token generated to authenticate your requests to the Gmail API.
This built-in Apps Script Gmail integration was not made for that use-case. The intended usage is to setup your own settings, or individual users that explicitly authorize your application to run on their behalf. This sendAs is there because one might have multiple Gmail signatures, depending on their selected send-as/from alias.
Note that simply authorizing an script with your GSuite admin account won't allow to the script to perform domain-wide operations. That'd be too dangerous, therefore the somewhat convoluted service-account setup is required.
Link to the relevant documentation
I want to get and set auto forwarding details for a user in the company domain. My admin made me a delegated admin so that I can fetch user data using AdminDirectory.Users.
However, when I try to fetch auto-forwarding/Label/Filter data for a user using their userID, the following error is thrown :
Delegation denied for some.user#domain.com
This is the line that evoked the error :
var labels = Gmail.Users.Labels.list(user.id);
It seems that this can be carried out by delegating domain-wide authority to a Service Account. However, when I tried the steps highlighted in this link : Link , the page asked me to select a project.
This script doesn't feature in those projects, I am not sure if it is because it is bound to Google Forms.
Any suggestions on how to give the same domain-wide authority privileges to this script?
Please help!!
To create a service account for the Google script:
Start in the Google Script IDE itself and from the menu choose Resources > Developer Console Project...
Click on the link labeled "This script is currently associated with project:" located near the top of the dialog.
From there click Credentials in the left navigation and then Create credentials > Service account key follow the flow to create a service account - for Role, choose Project > Service account actor.
You can then use the private key and client id from the downloaded .json file in your Google Script.
Note: You'll need to:
Get the G Suite domain admin to set up your client_id with the scopes you want. https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority
Use the Gmail API via UrlFetch because you can't make the Gmail advanced service use a different OAuth token. You can use the OAuth2 library for that and follow this example: https://github.com/googlesamples/apps-script-oauth2/blob/master/samples/GoogleServiceAccount.gs
First, make sure that you enable the domain-wide delegation for your service account which is stated in the link that you provide. Because the service account that you created needs to be granted access to the Google Apps domain’s user data that you want to access.
Also in your Apps Script code, go to the Resource -> Advance Google Service, make sure you enable all the Google Service that you use for your application that you create here.
For more information, check these threads:
Perform Google Apps Domain-Wide Delegation of Authority
Delegation Denied for ""
Trouble Implementing OAuth in Google Apps Script (Using Domain-Wide Delegation)
I want to authorize non-admin users to use AdminDirectory (part of Google apps Admin SDK) as a part of a google apps script. Basically I want the users to get a list of other users' full names based on their user name.
I understand I can do this using OAuth2 but I cannot find examples of Google apps script-code for Admin SDK-AdminDirectory.
I have created a service account and have and have my Client ID and key ID. What do I need to do next? I found this https://developers.google.com/api-client-library/javascript/features/authentication but I can't figure out how to get the authorization to work.
Here is a minimal version of my script: (It will produce the full name of user edutett0707#edu.kristinehamn.se for authorized users)
function grupplistor() {
var userinfo = AdminDirectory.Users.get("edutett0707#edu.kristinehamn.se");
Logger.log ([userinfo.name.fullName]);
}
Try:
function grupplistor() {
var userinfo = AdminDirectory.Users.get({
userKey: "edutett0707#edu.kristinehamn.se",
viewType: "domain_public"});
Logger.log ([userinfo.name.fullName]);
}
viewType domain_public tells the api to return information about the user that's shared to all other users.
Jay
You can try using creating the service account and its credentials.
You need to create a service account and its credentials. During this procedure you need to gather information that will be used later for the Google Apps domain-wide delegation of authority and in your code to authorize with your service account. The three items you need are your service account’s:
Client ID.
Private key file.
Email address.
Note: Only users with access to the Admin APIs can access the Admin SDK Directory API, therefore your service account needs to impersonate one of those users to access the Admin SDK Directory API.
Source:
https://github.com/googlesamples/apps-script-oauth2
https://github.com/Spencer-Easton/Apps-Script-Drive-Service-Account-Library
Hope this helps
I'm wondering if anyone has updated the following script for the new "Admin SDK Directory Service" as I currently get an error with the "UserManager" is not defined.
The original script is from this site (Thank you),
http://www.googleappsscript.org/home/force-google-apps-users-to-change-password-periodically
I'm not a developer but I have used this script before and it worked great.
This is the line that is giving you the error:
//get all users in domain
var users = UserManager.getAllUsers();
That API is now deprecated:
Google Documentation - google-apps/provisioning
In order to use the new API, it must be explicitly enabled in your Apps Script editor. And you will need a domain name.
Google Support - What is a domain?
If you have a domain name, you can use this example code, to get a list of all the users:
Apps Script Documentation - Admin SDK Directory Service
The code you were using would need to be totally re-written. It's not really what stackoverflow is meant for.