404 Resource Not Found: domain with Google Directory API
After a bit of documentation reading, there were two things that I needed to fix.
- I hadn't set up the proper authorization for my test service account.
You have to go to the Apps Console > Security > Advanced > Manage API client access and add the client url for your service account as well as any specific permissions that you want to add
- As seen in this question, it seems that you need to create a user object rather than just passing in parameters.
Here's my updated code:
# Authorization happens here ....
api = client.discovered_api('admin', 'directory_v1')
new_user = api.users.insert.request_schema.new(
name: { familyName: 'Testy', givenName: 'Testerson' },
primaryEmail: 'ttttesterson@<domain-redacted>.com',
password: 'password123'
)
result = client.execute(
api_method: api.users.insert,
body_object: new_user
)
Google API: 404 Domain not found
Okay, this was a very easy step to overlook but it was an extremely simple fix.
The issue here was that the domain for the account was not identified. I was under the impression that the service account was already attached to the domain but that is not the case. So the fix is just one line of code to add to the client to set it to a user that is in the domain (for my case).
The fix for me was to add:
$client->setSubject('account@domain.com');
to my getClient
method.
so now the method looks like:
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
function getClient() {
$client = new Google_Client();
$client->setApplicationName('TestingApp');
$client->setAuthConfig(CREDENTIALS_PATH);
$client->setScopes(SCOPES);
$client->setSubject('account@domain.com');
return $client;
}
I saw this mentioned in the API but it states it as optional. Hopefully this will help someone else too.
Google Admin SDK Directory API members.get() returns a 404 for member email, but succeeds for member id
After getting in touch with Google support, it turns out this is a known issue, but no current eta for a fix. We're expected to work around it by using the membership id instead of the email address as the memberKey
.
Resource not Found userKey Google Directory API using Service Account
Assuming you've already granted your service account domain-wide delegation, you'll need to have it impersonate an admin account using .setServiceAccountUser("admin@domain.com")
as shown in the service account documentation. Service accounts are not themselves domain admins, so they need to impersonate one in order to use the Admin SDK APIs.
Google Admin SDK error Resource Not Found: domain when trying to list existing users
Even when using a service account, you still need to "act as" a Google Apps user in the domain with the proper rights (e.g. a super admin). Try:
credentials = SignedJwtAssertionCredentials(client_email, private_key,
OAUTH_SCOPE, sub='admin@domain.com')
where admin@domain.com is the email of a super admin in your domain.
Google API: 404 Domain not found
Okay, this was a very easy step to overlook but it was an extremely simple fix.
The issue here was that the domain for the account was not identified. I was under the impression that the service account was already attached to the domain but that is not the case. So the fix is just one line of code to add to the client to set it to a user that is in the domain (for my case).
The fix for me was to add:
$client->setSubject('account@domain.com');
to my getClient
method.
so now the method looks like:
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
function getClient() {
$client = new Google_Client();
$client->setApplicationName('TestingApp');
$client->setAuthConfig(CREDENTIALS_PATH);
$client->setScopes(SCOPES);
$client->setSubject('account@domain.com');
return $client;
}
I saw this mentioned in the API but it states it as optional. Hopefully this will help someone else too.
Resource not found: domain while inserting a new user with Google API
I found the mistake. The domain should of course end with ".com".
The correct request is:
def insertNewUser(directory_service):
params = {
'name': {
'familyName': 'Testfamilyname',
'givenName': 'TestgivenName',
},
'password': 'testpassword',
'primaryEmail': 'testemail@mycompanydomain.com',
}
result = directory_service.users().insert(body=params).execute()
Does fixing a domain not found error require a G-Suite account
The Directory API only supports G Suite, Education and Government editions Google accounts.
Anyway, the error 404 is not related to your account status, but to a missing or wrong domain in your credentials. If this was your case you would get a 403 error "Not Authorized to access this resource/api"
For more info about the Directory API Prerequisties: https://developers.google.com/admin-sdk/directory/v1/guides/prerequisites
Related Topics
Getting a Rogue Iteration from My .Each Loop
Ruby, Rails Gem Install Error - Error: While Executing Gem ... (Encoding::Undefinedconversionerror)
Ruby: Length of a Line of a File in Bytes
Can You Have Multiple Versions of a Gem in a Gemfile
How to Force To_Yaml to Output Long Strings in Literal Block Style
New Way of Creating Hashes in Ruby 2.2.0
Calling Ruby Method Without Instantiating Class
Installing Ruby Using Rvm Fails, Without Trace
Rails: Allowing a Partial to Only Be Rendered Once
Hardcoded "Require 'Debug'" Can't Find the Sourcefile
Idiomatically Mock Openuri.Open_Uri with Minitest
Use [].Replace to Make a Copy of an Array
Should Repeat a Number of Times
How to Interpolate a Variable in a Ruby Regex
How to Escape the Vertical Bar When Doing Command Line with Ruby