java - 使用java中的REST api创建azure自动化帐户

标签 java api rest azure azure-automation

我正在尝试使用 java 中的 REST API 在 Azure 中创建自动化帐户。现在我可以列出图像,使用 java 中的 REST API 创建服务,但是当我尝试创建自动化帐户时,出现错误:

   Forbidden
   <Error xmlns="http://schemas.microsoft.com/windowsazure" 
   xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
   <Code>ForbiddenError</Code>
   <Message>The server failed to authenticate the request. Verify that the certificate is valid and is associated with this subscription.</Message>
   </Error> 

JAVA类:

  import java.io.IOException;
  import java.net.*;
  import java.security.*;
  import java.io.*;
  import java.security.KeyManagementException;
  import java.security.NoSuchAlgorithmException;
  import java.security.KeyStoreException;
  import java.security.UnrecoverableKeyException;
  import javax.net.ssl.HttpsURLConnection;
  import javax.net.ssl.KeyManagerFactory;
  import javax.net.ssl.SSLContext;
  import javax.net.ssl.SSLSocketFactory;
  import org.apache.commons.codec.binary.Base64;

  public class AzureRest
  {
        private static KeyStore getKeyStore(String keyStoreName, String password)
throws IOException
{
    KeyStore ks = null;
    FileInputStream fis = null;
    try {
        ks = KeyStore.getInstance("JKS");
        char[] passwordArray = password.toCharArray();
        fis = new java.io.FileInputStream(keyStoreName);
        ks.load(fis, passwordArray);
        fis.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    finally {
        if (fis != null) {
            fis.close();
        }
    }
    return ks;
}

    private static SSLSocketFactory getSSLSocketFactory(String keyStoreName, String password)
throws UnrecoverableKeyException, KeyStoreException, 
NoSuchAlgorithmException, KeyManagementException, IOException {

    KeyStore ks = getKeyStore(keyStoreName, password);
    KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
    keyManagerFactory.init(ks, password.toCharArray());

    SSLContext context = SSLContext.getInstance("TLS");
    context.init(keyManagerFactory.getKeyManagers(), null, new SecureRandom());

    return context.getSocketFactory();
}

private static String getStringFromInputStream(InputStream is) {

    BufferedReader br = null;
    StringBuilder sb = new StringBuilder();

    String line;
    try {
        br = new BufferedReader(new InputStreamReader(is));
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return sb.toString();
}

private static String processGetRequest(URL url, String keyStore, String keyStorePassword) throws UnrecoverableKeyException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException, IOException {
    SSLSocketFactory sslFactory = getSSLSocketFactory(keyStore, keyStorePassword);
    HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
    con.setSSLSocketFactory(sslFactory);
    con.setRequestMethod("GET");
    con.addRequestProperty("x-ms-version", "2013-08-01");
    InputStream responseStream = (InputStream) con.getContent();
    String response = getStringFromInputStream(responseStream);
    responseStream.close();
    return response;
}

private static int processPostRequest(URL url, byte[] data, String contentType, String keyStore, String keyStorePassword) throws UnrecoverableKeyException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException, IOException {
    SSLSocketFactory sslFactory = getSSLSocketFactory(keyStore, keyStorePassword);
    HttpsURLConnection con = null;
    con = (HttpsURLConnection) url.openConnection();
    con.setSSLSocketFactory(sslFactory);
    con.setDoOutput(true);
    con.setRequestMethod("POST");
    con.addRequestProperty("x-ms-version", "2013-08-01");
    con.setRequestProperty("Content-Length", String.valueOf(data.length));
    con.setRequestProperty("Content-Type", contentType);

    DataOutputStream requestStream = new DataOutputStream (con.getOutputStream());
    requestStream.write(data);
    requestStream.flush();
    requestStream.close();

    System.out.println(con.getResponseMessage());

    InputStream error = ((HttpURLConnection) con).getErrorStream(); 

    BufferedReader br = null;
    if (error == null) {
         InputStream inputstream = con.getInputStream();
          br = new BufferedReader(new InputStreamReader(inputstream));
    } else {
        br = new BufferedReader(new InputStreamReader(error));
    }
    String response = "";
    String nachricht;
    while ((nachricht = br.readLine()) != null){
          response += nachricht;
    }
    System.out.println(response);
    return con.getResponseCode();
}

private static int processDeleteRequest(URL url, String keyStore, String keyStorePassword) throws UnrecoverableKeyException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException, IOException {
    SSLSocketFactory sslFactory = getSSLSocketFactory(keyStore, keyStorePassword);
    HttpsURLConnection con = null;
    con = (HttpsURLConnection) url.openConnection();
    con.setSSLSocketFactory(sslFactory);
    con.setRequestMethod("DELETE");
    con.addRequestProperty("x-ms-version", "2013-08-01");
    return con.getResponseCode();
}

public static void getLocation(String keyStorePath, String keyStorePassword, String subscriptionId)
throws  UnrecoverableKeyException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException, IOException
{
    String url = "";
    // List locations
    url = String.format("https://management.core.windows.net/%s/locations", subscriptionId);
    String response = processGetRequest(new URL(url), keyStorePath, keyStorePassword);
    System.out.println(response);
}

public static void createCloudService(String keyStorePath, String keyStorePassword, String subscriptionId, String cloudService)
throws  UnrecoverableKeyException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException, IOException
{
    String url = "";
    // Create cloud service
    String label = "Cloud service in java";
    String description = "Cloud service in java";
    String location = "East US 2";
    url = String.format("https://management.core.windows.net/%s/services/hostedservices", subscriptionId);
    String requestBody = "<?xml version=\"1.0\" encoding=\"utf-8\"?><CreateHostedService xmlns=\"http://schemas.microsoft.com/windowsazure\"><ServiceName>%s</ServiceName><Label>%s</Label><Description>%s</Description><Location>%s</Location></CreateHostedService>";
    requestBody = String.format(requestBody, cloudService, Base64.encodeBase64String(label.getBytes()), description, location);
    int createResponseCode = processPostRequest(new URL(url), requestBody.getBytes(), "application/xml", keyStorePath, keyStorePassword);
    System.out.println("Created service: " + createResponseCode);
}

public static void deleteCloudService(String keyStorePath, String keyStorePassword, String subscriptionId, String cloudService)
throws  UnrecoverableKeyException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException, IOException
{
    String url = "";
    //Delete cloud service
    url = String.format("https://management.core.windows.net/%s/services/hostedservices/%s", subscriptionId, cloudService);
    int deleteResponseCode = processDeleteRequest(new URL(url), keyStorePath, keyStorePassword);
    System.out.println(deleteResponseCode);
}

public static void listImages(String keyStorePath, String keyStorePassword, String subscriptionId)
throws  UnrecoverableKeyException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException, IOException
{
    String url = "";
    String response = "";
    // List Images
    url = String.format("https://management.core.windows.net/%s/services/images", subscriptionId);
    response = processGetRequest(new URL(url), keyStorePath, keyStorePassword);
    System.out.println(response);
}

public static void listAutomationAccounts(String keyStorePath, String keyStorePassword, String subscriptionId)
throws  UnrecoverableKeyException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException, IOException
{
    String url = "";
    String response = "";
    // List automation accounts in a cloud service
    url = String.format("https://management.core.windows.net/%s/cloudServices/OaaSCSI6EGAZU6F6QTCK5XRVT45FKJC6RC7IQIQW3OPR7SVLE4ZPD4IQQQ-East-US?resourceType=AutomationAccount&detailLevel=Full&resourceProviderNamespace=automation", subscriptionId);
    response = processGetRequest(new URL(url), keyStorePath, keyStorePassword);
    System.out.println(response);
}

public static void checkCloudServiceAvailability(String keyStorePath, String keyStorePassword, String subscriptionId, String cloudService)
throws  UnrecoverableKeyException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException, IOException
{
    String url = "";
    String response = "";

    // Check service name availability
    url = String.format("https://management.core.windows.net/%s/services/hostedservices/operations/isavailable/%s", subscriptionId, cloudService);
    response = processGetRequest(new URL(url), keyStorePath, keyStorePassword);
    System.out.println(response);
}

public static void createAutomationAccount(String keyStorePath, String keyStorePassword, String subscriptionId)
throws  UnrecoverableKeyException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException, IOException
{
    String url = "";
    String response = "";
    // Create automation
    String automationName = "javaauto";
    String location = "East US 2";

    url = String.format("https://management.core.windows.net/%s/cloudServices/OaaSCSI6EGAZU6F6QTCK5XRVT45FKJC6RC7IQIQW3OPR7SVLE4ZPD4IQQQ-East-US/resources/automation/AutomationAccount/%s?resourceType=AutomationAccount&detailLevel=Full&resourceProviderNamespace=automation", subscriptionId, automationName);

    String requestBody = "<Response xmlns=\"http://schemas.microsoft.com/windowsazure\"><CloudServiceSettings><GeoRegion>East US 2</GeoRegion></CloudServiceSettings><SchemaVersion>1.0</SchemaVersion><Plan>Basic</Plan></Resource>";

    int createResponseCode = processPostRequest(new URL(url), requestBody.getBytes(), "application/xml", keyStorePath, keyStorePassword);

    System.out.println("CreateAutomationAccount " + createResponseCode);
}

// Create a published runbook
public static void createRunbook(String keyStorePath, String keyStorePassword, String subscriptionId)
throws  UnrecoverableKeyException, KeyManagementException, KeyStoreException, NoSuchAlgorithmException, IOException
{
    String url = "";
    String response = "";
    // Create runbook 
    String runbookName = "Write-HellowWorld";
    String cloudService = <CLOUD_SERVICE>;

    url = String.format("https://management.core.windows.net/%s/cloudServices/%s/resources/automation/~/automationAccounts/javaauto/runbooks/%s?api-version=2014-12-08", subscriptionId, cloudService, runbookName);
    String requestBody = "{ \"tags\":{ \"Testing\":\"show value\", \"Source\":\"TechNet Script Center\" }, \"properties\":{ \"description\":\"Hello world\", \"runbookType\":\"Script\", \"logProgress\":false, \"publishContentLink\":{ \"uri\":\"https://gallery.technet.microsoft.com/scriptcenter/The-Hello-World-of-Windows-81b69574/file/111354/1/Write-HelloWorld.ps1\", \"contentVersion\":\"1.0.0.0\", \"contentHash\":{ \"algorithm\":\"sha256\", \"value\":\"EqdfsYoVzERQZ3l69N55y1RcYDwkib2+2X+aGUSdr4Q=\" } } } }";

    int createResponseCode = processPutRequest(new URL(url), requestBody.getBytes(), "application/json", keyStorePath, keyStorePassword);
    System.out.println("created runbook:: " + createResponseCode);
}

public static void main(String[] args)
{
    try {
        String subscriptionId = "<Insert your subscription ID>"; 
        String keyStorePath = "KeyStore.jks";
        String keyStorePassword = "secret321";

        String cloudService = "1testjava";

        //listImages(keyStorePath, keyStorePassword, subscriptionId);
        listAutomationAccounts(keyStorePath, keyStorePassword, subscriptionId);
        checkCloudServiceAvailability(keyStorePath, keyStorePassword, subscriptionId, cloudService);
        deleteCloudService(keyStorePath, keyStorePassword, subscriptionId, cloudService);
        createCloudService(keyStorePath, keyStorePassword, subscriptionId, cloudService);
        createAutomationAccount(keyStorePath, keyStorePassword, subscriptionId);

        // This also throws an error
        createRunbook(keyStorePath, keyStorePassword, subscriptionId);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

为了运行此程序,我使用以下方法创建了一个 keystore 文件 Keystore.jks 及其 .cer 文件:

keytool -genkeypair -alias keyfile -keyalg RSA -keystore KeyStore.jks -keysize 2048 -storepass“secret321”

keytool -v -export -file SMAPI.cer -keystore KeyStore.jks -alias keyfile `

SMAPI.cer 已上传到 Window Azure 门户,设置 > 管理证书

最佳答案

我检查了你的代码,发现了如下两个错误。

  1. 请参阅链接https://msdn.microsoft.com/en-us/library/azure/mt163820.aspx ,用于创建自动化帐户的 REST API 是使用 Http PUT 方法,而不是 POST 方法。 所以我将您的函数“processPostRequest”复制到新函数“processPutRequest”,并替换“con.setRequestMethod("POST");”与“con.setRequestMethod(“PUT”);”并将其修复在函数“createAutomationAccount”中。

  2. 在函数“createAutomationAccount”中,请求正文 xml 的根标记名称是“Resource”,而不是“Response”。

在修复故障之前它将正常工作。

最诚挚的问候!

关于java - 使用java中的REST api创建azure自动化帐户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31858486/

相关文章:

c# - 如何指定API版本?

rest - Hub、Spoke 和 ESB 之间的区别

java - 使用 java 的 Apache NiFi REST Api

java - 奇怪的 XML 结构的验证

java - 无法实例化 Activity : Chronometer (Android Studio)

api - 维基百科的 MediaWiki api - 是否可以按标题搜索所有语言?

JavaScript ||将 API 与 ES6 模块结合使用

Android API 由 C 还是 Java 实现?

java - 为什么 byte += 1 编译但 byte = byte + 1 不编译?

java - 通常,当java返回 "The parameter is incorrect"时,这意味着什么