Add service accounts to public Google groups - google-drive-api

How can I add service accounts to public Google groups?
I wanted to add service accounts to public Google groups because they were linked to some directory in Google drive and let service accounts work with them. Is there any way to do so?
I don't know how to address service accounts to itself join public Google groups. And let them access the Google drive files.

Related

List Google Drive files on website from GSuite users

I have an organization with GSuite with various users. The organization is connected to a Google app, Google Drive API is enabled, and I have an API Key. I'm trying to list files in a particular folder on various users' Google Drives. For example, I want to list all the pdfs in a folder called "annual reports" under one email, and the Google Docs under another's, with clickable urls to everything. The folders are set to public (anyone on the web can view them), but I cannot figure out how to to do this with php on a public facing website. Basically, I want to be able to go to example.com and see a list of links to each pdf, and a list of links to each Google doc so that the public can view/download things from the organization.

Add users to Google Group using Google Apps Script

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

authorize non-admin users to use AdminDirectory using OAuth2

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

How do I turn on the new Google Spreadsheets for Service Accounts

I want to use the new Google Spreadsheets with a API service account. It is easy to turn on for a personal account but how do I turn it on for a service account?
There is no programmatic method to create new sheets and service accounts cannot be logged into interactively. Thus new sheets cannot be created by a service account. You can share a new sheet with a service account however this is somewhat pointless as you can't access new sheets via the Spreadsheet api.

Access Google+ Data in Google Contacts

If a contact has a public Google+ profile and this data appears in your Google Contacts, is it possible to access that data via Google Apps Script? If so, how?
For example, I add a contact with only a full name and an email address. That contact has a public Google+ profile that includes a phone number. Their phone number will show up in my Google Contacts (including a search from Google Contacts) but the following code will not return their phone number:
var contacts = ContactsApp.getContacts();
for (i in contacts) {
phones = contacts[i].getPhones();
for (j in phones) {
Logger.log(phones[j].getPhoneNumber());
}
}
How do I access the Google+ phone number via Google Apps Script?
It is not possible to retrieve this information. At least, not directly via Google Apps Script Contacts Services.
I also think that this should be possible. Please open an enhancement request on Apps Script issue tracker.