Skip to content
This repository was archived by the owner on Jun 16, 2025. It is now read-only.
This repository was archived by the owner on Jun 16, 2025. It is now read-only.

Request event interface error 401 to access in application mode #17

@1914709715

Description

@1914709715

I am using Java to make calls using HTTPS

Here is the code I have written

public static void main(String[] args) throws Exception { try{ app = ConfidentialClientApplication.builder( clientId, ClientCredentialFactory.createFromSecret(secret)) .authority(authority) .build(); IAuthenticationResult result = getAccessTokenByClientCredentialGrant(); // String usersListFromGraph = getUsersListFromGraph(result.accessToken()); String hy= createHy(result.accessToken()); System.out.println("hy in the Tenant = " + hy); //System.out.println("Users in the Tenant = " + usersListFromGraph); System.out.println("Press any key to exit ..."); System.in.read(); }catch (Exception e){ throw e; } }

private static IAuthenticationResult getAccessTokenByClientCredentialGrant() throws Exception { // With client credentials flows the scope is ALWAYS of the shape "resource/.default", as the // application permissions need to be set statically (in the portal), and then granted by a tenant administrator ClientCredentialParameters clientCredentialParam = ClientCredentialParameters.builder( Collections.singleton(scope)) .build(); CompletableFuture future = app.acquireToken(clientCredentialParam); return future.get(); }

public static String createHy(String accessToken) throws IOException { String json="{"name": "Volunteer"}"; String urlPath = "https://graph.microsoft.com/v1.0/users/2af28489-0593-4fc6-97a1-d1df4c681f01/calendars"; URL url = new URL(urlPath); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Authorization", "Bearer " + accessToken); conn.setRequestProperty("Content-type","application/json"); conn.setDoOutput(true); try (OutputStream os = conn.getOutputStream()) { byte[] input = json.getBytes("utf-8"); os.write(input, 0, input.length); } try (BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"))) { String responseLine; StringBuilder response = new StringBuilder(); while ((responseLine = br.readLine()) != null) { response.append(responseLine.trim()); } System.out.println("Response: " + response.toString()); return response.toString(); } }

Image is no longer available.

Is it because there was a problem with obtaining my token that I have been granted the correct permissions but still reported 401 when creating a calendar

Image

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions