从谷歌开发者控制台创建了一个项目并创建了一个服务帐户.下载了密钥库.我需要使用google admin sdk来创建/删除/访问用户.我看到了Admin SDK ON API和Auth-> API.由于范围错误而无法获得授权.
public boolean makeConnectionWithGoogleAPI(){
try{
List<String> scopes = Arrays.asList("https://www.googleapis.com/auth/admin.directory.user",
"https://www.googleapis.com/auth/admin.directory.user.readonly");
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(clientEmail)
.setServiceAccountUser(userId)
.setServiceAccountScopes(scopes)
.setServiceAccountPrivateKeyFromP12File(
new java.io.File(privateKeyStoreLocation))
.build();
Directory admin =
new Directory.Builder(httpTransport, jsonFactory, null)
.setHttpRequestInitializer(credential).build();
Directory.Users.List list = admin.users().list();
Users users = list.execute();
List<User> listUsers=users.getUsers();
for(User user:listUsers){
System.out.println(user.getId());
}
return true;
}catch(Exception e){
e.printStackTrace();
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
com.google.api.client.auth.oauth2.TokenResponseException:400 Bad Request {"error":"access_denied","error_description":"不允许请求的范围:https ://www.googleapis.com/auth/admin. directory.user …
文档对此有点不清楚.我真的可以这样做吗?到目前为止,我见过的唯一示例来自Google文档,该文档使用GoogleAuthorizationCodeFlow类来获取授权.我已经看到了一些使用服务帐户更新和检索用户列表的示例,但没有任何内容可供创建.这可能吗?如果是这样,有没有人知道任何漂浮的例子?我似乎无法找到解决它的任何东西.
我正在使用服务帐户模型和Google的Java API来检索和修改用户.
我可以使用类似于Google示例的代码成功创建GoogleCredential对象:
GoogleCredential googleCredential = new GoogleCredentialBuilder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setServiceAccountUser(SERVICE_ACCOUNT_USER)
.setServiceAccountPrivateKeyFromP12File(P12_PRIVATE_KEY_FILE)
.setServiceAccountScopes(Collections.singleton(GLOBAL_USER_AND_ALIAS_SCOPE)
.build();
Run Code Online (Sandbox Code Playgroud)
我在任何示例中都没有提到我必须明确创建一个访问令牌,所以我一直假设上面的代码处理它.真的吗?
之后,我成功创建了一个Directory实例,然后尝试检索特定用户:
User user = new User();
user = directory.users().get(uid).execute();
Run Code Online (Sandbox Code Playgroud)
失败了,抛出了NullPointerException.
当我在调用之前检查GoogleCredential对象以获取用户对象时,它似乎不包含访问令牌:
accessToken = null
refreshToken = null
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
如何使用服务帐户模型获取访问令牌?
提前致谢.
我不相信有太多话要说.每当我将"whoCanPostMessage"设置从"ALL_MEMBERS_CAN_POST"修改或更新为"ALL_MANAGERS_CAN_POST"时,Google API后端就会忽略它.
此问题出现于06-06-2016.
我创建了我的服务帐户,获得了private_key并委托了域范围的权限.
这是我的代码尝试使用服务帐户进行身份验证,但获得相同的"无效的令牌格式错误":
session_start();
include_once 'google-api-php/vendor/autoload.php';
function getClient() {
$client = new Google_Client();
$client->setApplicationName('theName');
$client->setScopes('https://www.googleapis.com/auth/admin.directory.user.readonly');
$client->setAccessType('offline');
$client->setSubject('admin@domain.com');
$client->setAuthConfig('private_key.json');
// Load previously authorized credentials from a file.
$credentialsPath = 'private_key.json';
if (file_exists($credentialsPath)) {
$accessToken = json_decode(file_get_contents($credentialsPath), true);
}
else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
// Store the credentials to disk. …Run Code Online (Sandbox Code Playgroud) php oauth-2.0 google-api-php-client service-accounts google-admin-sdk
我正在使用库 google/google-api-php-client 并且我正在使用服务帐户创建身份验证。
我已经在https://console.developers.google.com/ 中创建了服务帐户,并将域范围的权限添加到了我的服务帐户。
但我返回我要求以下内容:
Google_Service_Exception in REST.php line 118:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "forbidden",
"message": "Not Authorized to access this resource/api"
}
],
"code": 403,
"message": "Not Authorized to access this resource/api"
}
}
Run Code Online (Sandbox Code Playgroud)
我的功能:
$client = new \Google_Client();
$credentials_file = base_path() . '/service-account.json';
if ($credentials_file = $this->checkServiceAccountCredentialsFile($credentials_file)) {
$client->setAuthConfig($credentials_file);
}
$client->setApplicationName("app-name");
$client->setScopes([
'https://www.googleapis.com/auth/admin.directory.user',
'https://www.googleapis.com/auth/admin.directory.user.readonly'
]);
$service = new \Google_Service_Directory($client);
$userKey = 'user@domain.com';
$results = $service->users->get($userKey);
var_dump($results);
Run Code Online (Sandbox Code Playgroud)
如果我尝试在这里工作。 https://developers.google.com/admin-sdk/directory/v1/reference/users/get#auth
我正在使用带有googleapis和google-auth-library包的节点来访问 G-Suite 域的用户。为此,创建了一个启用了域范围委派的服务帐户:
域管理员授予了对服务帐户的访问权限以访问以下范围:
"https://www.googleapis.com/auth/admin.directory.group.readonly",
"https://www.googleapis.com/auth/admin.directory.group.member.readonly",
"https://www.googleapis.com/auth/admin.directory.user.readonly"
Run Code Online (Sandbox Code Playgroud)
我的代码如下所示:
"https://www.googleapis.com/auth/admin.directory.group.readonly",
"https://www.googleapis.com/auth/admin.directory.group.member.readonly",
"https://www.googleapis.com/auth/admin.directory.user.readonly"
Run Code Online (Sandbox Code Playgroud)
JWT 客户端使用为服务帐户收到的凭据进行初始化。无论如何,客户端会返回以下消息:Not Authorized to access this resource/api
Google API 到期日期是 1 小时,问题是我使用 API 是为了允许用户使用管理 SDK 功能(列出群组、将成员添加到群组等)
没有人可以在一小时内完成任何一项工作,这将要求用户每天多次登录他们的帐户来管理他们的群组。如果您只想使用 Google 对用户进行身份验证,那么 1 小时的到期日期是不错的选择。
如何增加它或有任何解决方法?我错过了什么吗?
google-api google-api-php-client google-admin-sdk google-php-sdk google-workspace
我使用Google管理SDK中的目录API来管理Google域中的用户.我正在寻找一种方法来列出域中特定组织的用户,但我找不到任何关于如何实现这一目标的示例.根据文档https://developers.google.com/admin-sdk/directory/v1/reference/users/list,唯一有效的查询属性是email,familyName和givenName.我今天使用的解决方法是获取域中的所有用户,然后过滤响应.
我正在使用示例脚本来创建用户,但它不断收到此错误 ReferenceError: "AdminDirectory" is not defined
/**
* Adds a new user to the domain, including only the required information. For
* the full list of user fields, see the API's reference documentation:
* @see https://developers.google.com/admin-sdk/directory/v1/reference/users/insert
*/
function addUser() {
var user = {
primaryEmail: 'liz@mydomain.com',
name: {
givenName: 'Elizabeth',
familyName: 'Smith',
},
// Generate a random password string.
password: Math.random().toString(36),
};
user = AdminDirectory.Users.insert(user);
Logger.log('User %s created with ID %s.', user.primaryEmail, user.id);
}
Run Code Online (Sandbox Code Playgroud) 我的项目 - Node.js 和 Firebase 存在问题。
我想添加管理员角色,因此我根据文档使用自定义声明。我在云函数中有3个函数:
exports.addAdminRole = functions.https.onCall((data, context) => {
return admin.auth().setCustomUserClaims(context.auth.uid, {
admin: true
}).then(() =>'Success!')
.catch(err => err)
})
exports.getUserRecord = functions.https.onCall((data, context) => {
return admin.auth().getUser(context.auth.uid)
.then(userRecord => userRecord.customClaims)
.catch(err => err)
})
exports.deleteAdmin = functions.https.onCall((data, context) => {
return admin.auth().setCustomUserClaims(context.auth.uid, null)
.then(() => 'Deleted admin!')
.catch(err => err)
})
Run Code Online (Sandbox Code Playgroud)
我直接在客户端中调用函数(可调用 http)并且 addAdminRole 返回“成功!” 似乎有效。getUserRecord 似乎也能正常工作,并返回 {admin: true} 作为我的自定义声明。
现在来说说问题。我定义了一个函数来获取客户端的用户声明,如文档中提到的:
getRecords() {
firebaseInstance.auth.currentUser.getIdTokenResult()
.then(idTokenResult => {console.log("ADMIN:", idTokenResult)})
.catch(err => console.error(err))
}
Run Code Online (Sandbox Code Playgroud)
(firebaseInstance已导入并且在整个项目中运行良好)
我不明白为什么,但客户端中的代码返回一个具有 Claims …
node.js firebase google-admin-sdk firebase-authentication google-cloud-functions
google-admin-sdk ×11
node.js ×2
php ×2
firebase ×1
google-api ×1
google-apps ×1
java ×1
oauth-2.0 ×1