Java Google Admin SDK API:未授权访问此资源/API
我想从谷歌目录API中删除用户。删除时收到错误消息 com.google.api.client.googleapis.json.GoogleJsonResponseException:403禁止“未授权访问此资源/api” 但是,我的代码在插入用户时运行良好。我有超级管理员帐户,我使用相同的目录实例插入和删除用户。当我试图删除用户时,它确实起作用 请帮帮我。提前谢谢Java Google Admin SDK API:未授权访问此资源/API,java,google-api,google-apps,google-admin-sdk,google-directory-api,Java,Google Api,Google Apps,Google Admin Sdk,Google Directory Api,我想从谷歌目录API中删除用户。删除时收到错误消息 com.google.api.client.googleapis.json.GoogleJsonResponseException:403禁止“未授权访问此资源/api” 但是,我的代码在插入用户时运行良好。我有超级管理员帐户,我使用相同的目录实例插入和删除用户。当我试图删除用户时,它确实起作用 请帮帮我。提前谢谢 public class UserDisable { private static final Logger LOGGER =
public class UserDisable {
private static final Logger LOGGER = Logger.getLogger(EmployeeAPI.class);
/** Application name. */
private static final String APPLICATION_NAME = "User Account Creation";
/** Directory to store user credentials for this application. */
private static final java.io.File DATA_STORE_DIR = new java.io.File(
System.getProperty("user.home"),
".credentials/admin-directory_v1-java-quickstart");
/** Global instance of the {@link FileDataStoreFactory}. */
private static FileDataStoreFactory DATA_STORE_FACTORY;
/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY = JacksonFactory
.getDefaultInstance();
/** Global instance of the HTTP transport. */
private static HttpTransport HTTP_TRANSPORT;
private static final Random randomGenerator = new Random();
static Directory directoryClient = null;
private static final List<String> SCOPES = Arrays.asList(
DirectoryScopes.ADMIN_DIRECTORY_USER, DirectoryScopes.ADMIN_DIRECTORY_GROUP);
static {
try {
HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR);
} catch (Throwable t) {
System.exit(1);
}
}
/**
* Creates an authorized Credential object.
*
* @return an authorized Credential object.
* @throws IOException
*/
public static Credential authorize() throws IOException {
// Load client secrets.
InputStream in = UserDisable.class
.getResourceAsStream("/client_secret.json");
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(
JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(DATA_STORE_FACTORY)
.setAccessType("offline").build();
Credential credential = new AuthorizationCodeInstalledApp(flow,
new LocalServerReceiver()).authorize("user");
LOGGER.info("Credentials saved to "
+ DATA_STORE_DIR.getAbsolutePath());
return credential;
}
/**
* Build and return an authorized Admin SDK Directory client service.
*
* @return an authorized Directory client service
* @throws IOException
*/
public static Directory getDirectoryService() throws IOException {
Credential credential = authorize();
return new Directory.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME).build();
}
public static void demo(){
try {
directoryClient = getDirectoryService();
removeUser(directoryClient, "Anderson.Paul2@abc.com");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* removeGroup removes a group from Google.
* @param directoryClient a Directory (service) object
* @param userKey an identifier for a user (e-mail address is the most popular)
* @throws IOException
*/
public static void removeUser(Directory directoryClient, String userKey) throws IOException {
LOGGER.info("removeUser() - {}"+ userKey);
try {
directoryClient.users().delete(userKey).execute();
} catch (IOException e) {
LOGGER.info("An unknown error occurred: " + e);
}
}
该消息表示您正在进行身份验证的用户没有删除用户的权限。@DaImTo:但我正在通过此超级管理员帐户插入新用户。请尝试重新验证您的代码和应用程序。如果您在验证后添加了ADMIN\u DIRECTORY\u用户范围,并且仍在使用之前的刷新令牌,这也会导致错误。我觉得你的代码不错。我没有管理员帐户,所以无法测试它。@DaImTo:我也这么做了。但同样的错误仍然存在。请尝试执行用户列表。请确保您没有将用户拼写错误?
com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
"code" : 403,
"errors" : [ {
"domain" : "global",
"message" : "Not Authorized to access this resource/api",
"reason" : "forbidden"
}
],
"message" : "Not Authorized to access this resource/api"
}