我可以使用java应用程序或springrestapi触发或调用azurewebjobs吗

soat7uwm  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(244)

我想触发或调用一个azurewebjob使用我的java应用程序,这是一个简单的spring启动应用程序。
我有一些问题,比如我们可以使用 Spring Rest API 打电话?
如果是,那怎么办。webjob还需要用户名和密码。
我试过这样的方法:

@GetMapping
public String methodCall(@RequestHeader String username, @RequestHeader String password) {
    String ApiUrl = "https://myapp.scm.azurewebsites.net/api/triggeredwebjobs/webjob";
    RestTemplate resetTemplate = new RestTemplate();

    StringBuilder sb = new StringBuilder().append(ApiUrl).append(username).append(password);

    String response = resetTemplate.getForObject(sb.toString(), String.class);

    System.out.println("response:----------" + response);

    return response;
}
emeijp43

emeijp431#

如果要运行触发的azure web作业,可以使用azure rest api

POST https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Web/sites/{name}/triggeredwebjobs/{webJobName}/run?api-version=2019-08-01

Authorization: Bearer <access token>

例如
创建服务主体并将参与者角色分配给sp
代码

private static String authority = "https://login.microsoftonline.com/<tenantid>/";
private static String clientId="sp clientId";
private static String secret="sp client secret";
private static  String scope="https://management.azure.com/.default";

 try {
            // use msal4j to get token
            ConfidentialClientApplication app = ConfidentialClientApplication.builder(
                    clientId,
                    ClientCredentialFactory.createFromSecret(secret))
                    .authority(authority)
                    .build();

            ClientCredentialParameters clientCredentialParam = ClientCredentialParameters.builder(
                    Collections.singleton(scope))
                    .build();
            CompletableFuture<IAuthenticationResult> future = app.acquireToken(clientCredentialParam);

            HttpHeaders headers = new HttpHeaders();
            headers.set("Authorization", "Bearer " + future.get().accessToken());
            HttpEntity<String> entity = new HttpEntity<String>("",headers);
            String url ="https://management.azure.com/subscriptions/e5b0fcfa-e859-43f3-8d84-5e5fe29f4c68/resourceGroups/0730BowmanWindow/providers/Microsoft.Web/sites/bowman0210webapp/triggeredwebjobs/test/run?api-version=2019-08-01";
            String res= restTemplate.postForObject(url,entity,String.class);
            return res;
        } catch (MalformedURLException e) {
          throw e;
        } catch (InterruptedException e) {
            throw e;
        } catch (ExecutionException e) {
            throw e;
        }finally {
            return "cannot start";
        }

相关问题