Java Android管理API服务文件分配
我正在使用localhost作为回调url测试android管理API。我遵循这个url的每一步。 现在我被困在原地。。根据本指南,我从服务帐户下载json文件。现在我复制那个json文件并保存在我项目的app文件夹中。 这是我的enterprise.json文件 我只是在位置字符串中将文件夹位置设为enterprise.json 这是我的密码Java Android管理API服务文件分配,java,android,json,android-management-api,Java,Android,Json,Android Management Api,我正在使用localhost作为回调url测试android管理API。我遵循这个url的每一步。 现在我被困在原地。。根据本指南,我从服务帐户下载json文件。现在我复制那个json文件并保存在我项目的app文件夹中。 这是我的enterprise.json文件 我只是在位置字符串中将文件夹位置设为enterprise.json 这是我的密码 private static final String PROJECT_ID = "enterprise-271814"; private s
private static final String PROJECT_ID = "enterprise-271814";
private static final String SERVICE_ACCOUNT_CREDENTIAL_FILE =
"enterprise.json";
private static final String POLICY_ID = "samplePolicy";
/** The package name of the COSU app. */
private static final String COSU_APP_PACKAGE_NAME =
"com.ariaware.devicepoliceycontroller";
/** The OAuth scope for the Android Management API. */
private static final String OAUTH_SCOPE =
"https://www.googleapis.com/auth/androidmanagement";
private static final String APP_NAME = "Device Policey Controller";
private final AndroidManagement androidManagementClient;
public Sample(AndroidManagement androidManagementClient) {
this.androidManagementClient = androidManagementClient;
}
public void run() throws IOException {
// Create an enterprise. If you've already created an enterprise, the
// createEnterprise call can be commented out and replaced with your
// enterprise name.
String enterpriseName = createEnterprise();
System.out.println("Enterprise created with name: " + enterpriseName);
// Set the policy to be used by the device.
setPolicy(enterpriseName, POLICY_ID, getCosuPolicy());
// Create an enrollment token to enroll the device.
String token = createEnrollmentToken(enterpriseName, POLICY_ID);
System.out.println("Enrollment token (to be typed on device): " + token);
// List some of the devices for the enterprise. There will be no devices for
// a newly created enterprise, but you can run the app again with an
// existing enterprise after enrolling a device.
List<Device> devices = listDevices(enterpriseName);
for (Device device : devices) {
System.out.println("Found device with name: " + device.getName());
}
// If there are any devices, reboot one.
if (devices.isEmpty()) {
System.out.println("No devices found.");
} else {
rebootDevice(devices.get(0));
}
}
public static AndroidManagement getAndroidManagementClient()
throws IOException, GeneralSecurityException {
try (FileInputStream input =
new FileInputStream(SERVICE_ACCOUNT_CREDENTIAL_FILE)) {
GoogleCredential credential =
GoogleCredential.fromStream(input)
.createScoped(Collections.singleton(OAUTH_SCOPE));
return new AndroidManagement.Builder(
GoogleNetHttpTransport.newTrustedTransport(),
JacksonFactory.getDefaultInstance(),
credential)
.setApplicationName(APP_NAME)
.build();
}
}
private String createEnterprise() throws IOException {
// Initiate signup process.
System.out.println("Creating signup URL...");
SignupUrl signupUrl =
androidManagementClient
.signupUrls()
.create()
.setProjectId(PROJECT_ID)
.setCallbackUrl("https://localhost:9999")
.execute();
System.out.print(
"To sign up for a new enterprise, open this URL in your browser: ");
System.out.println(signupUrl.getUrl());
System.out.println(
"After signup, you will see an error page in the browser.");
System.out.print(
"Paste the enterpriseToken value from the error page URL here: ");
String enterpriseToken =
new BufferedReader(new InputStreamReader(System.in)).readLine();
// Create the enterprise.
System.out.println("Creating enterprise...");
return androidManagementClient
.enterprises()
.create(new Enterprise())
.setProjectId(PROJECT_ID)
.setSignupUrlName(signupUrl.getName())
.setEnterpriseToken(enterpriseToken)
.execute()
.getName();
}
private Policy getCosuPolicy() {
List<String> categories = new ArrayList<>();
categories.add("android.intent.category.HOME");
categories.add("android.intent.category.DEFAULT");
return new Policy()
.setApplications(
Collections.singletonList(
new ApplicationPolicy()
.setPackageName(COSU_APP_PACKAGE_NAME)
.setInstallType("FORCE_INSTALLED")
.setDefaultPermissionPolicy("GRANT")
.setLockTaskAllowed(true)))
.setPersistentPreferredActivities(
Collections.singletonList(
new PersistentPreferredActivity()
.setReceiverActivity(COSU_APP_PACKAGE_NAME)
.setActions(
Collections.singletonList("android.intent.action.MAIN"))
.setCategories(categories)))
.setKeyguardDisabled(true)
.setStatusBarDisabled(true);
}
private void setPolicy(String enterpriseName, String policyId, Policy policy)
throws IOException {
System.out.println("Setting policy...");
String name = enterpriseName + "/policies/" + policyId;
androidManagementClient
.enterprises()
.policies()
.patch(name, policy)
.execute();
}
private String createEnrollmentToken(String enterpriseName, String policyId)
throws IOException {
System.out.println("Creating enrollment token...");
EnrollmentToken token =
new EnrollmentToken().setPolicyName(policyId).setDuration("86400s");
return androidManagementClient
.enterprises()
.enrollmentTokens()
.create(enterpriseName, token)
.execute()
.getValue();
}
private List<Device> listDevices(String enterpriseName) throws IOException {
System.out.println("Listing devices...");
ListDevicesResponse response =
androidManagementClient
.enterprises()
.devices()
.list(enterpriseName)
.execute();
return response.getDevices() ==null
? new ArrayList<Device>() : response.getDevices();
}
private void rebootDevice(Device device) throws IOException {
System.out.println(
"Sending reboot command to " + device.getName() + "...");
Command command = new Command().setType("REBOOT");
androidManagementClient
.enterprises()
.devices()
.issueCommand(device.getName(), command)
.execute();
}
私有静态最终字符串项目\u ID=“enterprise-271814”;
私有静态最终字符串服务\u帐户\u凭证\u文件=
“enterprise.json”;
私有静态最终字符串策略\u ID=“samplePolicy”;
/**COSU应用程序的包名称*/
私有静态最终字符串COSU\u应用程序\u包\u名称=
“com.ariaware.devicepoliceycontroller”;
/**Android管理API的OAuth范围*/
私有静态最终字符串OAUTH_作用域=
"https://www.googleapis.com/auth/androidmanagement";
私有静态最终字符串APP_NAME=“设备策略控制器”;
私人最终AndroidManagement androidManagementClient;
公共样本(AndroidManagement androidManagementClient){
this.androidManagementClient=androidManagementClient;
}
public void run()引发IOException{
//创建企业。如果您已经创建了企业
//createEnterprise调用可以被注释掉并替换为您的
//企业名称。
字符串enterpriseName=createEnterprise();
System.out.println(“使用名称创建的企业:“+enterpriseName”);
//设置设备要使用的策略。
setPolicy(enterpriseName,POLICY_ID,getCosuPolicy());
//创建注册令牌以注册设备。
字符串令牌=createEnrollmentToken(企业名称,策略ID);
System.out.println(“注册令牌(在设备上键入):”+令牌);
//列出该企业的一些设备。将没有用于该企业的设备
//新创建的企业,但您可以使用
//注册设备后的现有企业。
列表设备=列表设备(enterpriseName);
用于(设备:设备){
System.out.println(“找到名为:+device.getName()的设备);
}
//如果有任何设备,请重新启动一个。
if(devices.isEmpty()){
System.out.println(“未找到设备”);
}否则{
重新启动设备(devices.get(0));
}
}
公共静态AndroidManagement getAndroidManagementClient()
抛出IOException,GeneralSecurityException{
try(FileInputStream输入)=
新文件输入流(服务\帐户\凭证\文件)){
谷歌凭证=
GoogleCredential.fromStream(输入)
.createScope(Collections.singleton(OAUTH_范围));
返回新的AndroidManagement.Builder(
GoogleNetHttpTransport.newTrustedTransport(),
JacksonFactory.getDefaultInstance(),
证书)
.setApplicationName(应用程序名称)
.build();
}
}
私有字符串createEnterprise()引发IOException{
//启动注册流程。
System.out.println(“创建注册URL…”);
注册URL注册URL=
androidManagementClient
.signupurl()
.create()
.setProjectId(项目ID)
.setCallbackUrl(“https://localhost:9999")
.execute();
系统输出(
要注册新企业,请在浏览器中打开此URL:);
System.out.println(signupUrl.getUrl());
System.out.println(
“注册后,您将在浏览器中看到一个错误页面。”);
系统输出(
“将错误页面URL中的enterpriseToken值粘贴到此处:”;
弦企业网=
新的BufferedReader(新的InputStreamReader(System.in)).readLine();
//创建企业。
System.out.println(“创建企业…”);
返回androidManagementClient
.企业()
.create(新企业())
.setProjectId(项目ID)
.setSignupUrlName(signupUrl.getName())
.setEnterpriseToken(enterpriseToken)
.execute()
.getName();
}
私人政策getCosuPolicy(){
列表类别=新建ArrayList();
categories.add(“android.intent.categority.HOME”);
categories.add(“android.intent.categority.DEFAULT”);
返回新策略()
.setApplications(
集合。单音列表(
新应用程序策略()
.setPackageName(COSU\u应用程序\u程序包\u名称)
.setInstallType(“强制安装”)
.setDefaultPermissionPolicy(“授权”)
.setLockTaskAllowed(true)))
.setPersistentPreferredActivities(
集合。单音列表(
新的PersistentPreferredActivity()
.setReceiverActivity(COSU\应用程序\包\名称)
.setActions(
Collections.singletonList(“android.intent.action.MAIN”))
.setCategories(categories)))
.setkeyguardisabled(真)
.setStatusBarDisabled(真);
}
私有void setPolicy(字符串enterpriseName、字符串policyId、策略策略)
抛出IOException{
System.out.p