Skip to content

Access Meeting Creation Return 401 #3856

Open
@1914709715

Description

@1914709715

public static void createMeeting(String accessToken, String userId) throws IOException {
OkHttpClient client = new OkHttpClient();

    // 创建请求体,设置会议详细信息
    String json = "{"
            + "\"subject\": \"Team Meeting\","
            + "\"start\": {"
            + "   \"dateTime\": \"2025-05-25T09:00:00\","
            + "   \"timeZone\": \"UTC\""
            + "},"
            + "\"end\": {"
            + "   \"dateTime\": \"2025-05-25T10:00:00\","
            + "   \"timeZone\": \"UTC\""
            + "},"
            + "\"attendees\": ["
            + "   {"
            + "       \"emailAddress\": {"
            + "           \"address\": \"***********\","
            + "           \"name\": \"Attendee Name\""
            + "       },"
            + "       \"type\": \"required\""
            + "   }"
            + "],"
            + "\"location\": {"
            + "   \"displayName\": \"Conference Room 1\""
            + "},"
            + "\"body\": {"
            + "   \"contentType\": \"HTML\","
            + "   \"content\": \"Agenda: Discuss project updates.\""
            + "}"
            + "}";

    RequestBody body = RequestBody.create(MediaType.parse("application/json"), json);

    // 修改这里,使用 /users/{userId}/events
    String url = "https://graph.microsoft.com/v1.0/users/" + userId + "/events";

    // 创建请求
    Request request = new Request.Builder()
            .url(url)  // 使用特定用户的 /events 端点
            .addHeader("Authorization", "Bearer " + accessToken)
            .post(body)
            .build();

    // 发送请求并处理响应
    try (Response response = client.newCall(request).execute()) {
        if (response.isSuccessful()) {
            System.out.println("Meeting created successfully!");
            System.out.println(response.body().string());
        } else {
            System.out.println("Failed to create meeting: " + response.code());
            System.out.println(response.body().string());
        }
    }
}
public static String getAccessToken() throws IOException {
    OkHttpClient client = new OkHttpClient();

    // 请求体,包含应用的凭证
    String requestBody = "client_id=" + CLIENT_ID
            + "&client_secret=" + CLIENT_SECRET
            + "&scope=https://graph.microsoft.com/.default"  // .default 表示应用所拥有的所有权限
            + "&grant_type=client_credentials";  // 客户端凭证流

    RequestBody body = RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), requestBody);

    // 请求获取 token
    Request request = new Request.Builder()
            .url("https://login.microsoftonline.com/" + "*************" + "/oauth2/v2.0/token")
            .post(body)
            .build();

    // 发送请求并处理响应
    try (Response response = client.newCall(request).execute()) {
        if (response.isSuccessful()) {
            String jsonResponse = response.body().string();
            // 从响应中提取 access_token
            String accessToken = jsonResponse.split("\"access_token\":\"")[1].split("\"")[0];
            return accessToken;
        } else {
            throw new IOException("Failed to get access token: " + response.body().string());
        }
    }
}
public static void main(String[] args) {
    try {
        String accessToken = getAccessToken(); // 获取 access token
        createMeeting(accessToken,"****************"); // 创建会议
    } catch (IOException e) {
        e.printStackTrace();
    }
}

I created a token request using okhttp3 using client segment credentials, but can you help me check the conference report 401

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions