Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Microsoft graph api 使用Graph API向团队中的特定用户发送消息_Microsoft Graph Api_Microsoft Teams_Microsoft Graph Teams - Fatal编程技术网

Microsoft graph api 使用Graph API向团队中的特定用户发送消息

Microsoft graph api 使用Graph API向团队中的特定用户发送消息,microsoft-graph-api,microsoft-teams,microsoft-graph-teams,Microsoft Graph Api,Microsoft Teams,Microsoft Graph Teams,通过Graph API向团队中的特定用户发送消息的任何端点?(由于清晰和添加了自定义请求而编辑) 您可以通过Graph API向私人用户发送消息,但存在一个问题,即无法通过Graph API在两个用户之间创建新的聊天。这意味着,如果要从用户向用户发送消息,则聊天必须已经存在。(必须先通过MSTeams客户端交换消息,才能进行聊天) 所以,确保你有一个开放的聊天 如果是,请查看此MSDoc(此文档说明如何列出用户的聊天记录): 列出所有聊天记录后,您可以查看此MSDoc(本文档说明如何向用户发送

通过Graph API向团队中的特定用户发送消息的任何端点?

(由于清晰和添加了自定义请求而编辑)

您可以通过Graph API向私人用户发送消息,但存在一个问题,即无法通过Graph API在两个用户之间创建新的聊天。这意味着,如果要从用户向用户发送消息,则聊天必须已经存在。(必须先通过MSTeams客户端交换消息,才能进行聊天)

所以,确保你有一个开放的聊天

如果是,请查看此MSDoc(此文档说明如何列出用户的聊天记录):

列出所有聊天记录后,您可以查看此MSDoc(本文档说明如何向用户发送消息):

注意权限!对于发送消息和列表聊天,到目前为止只有委派权限这些呼叫仅在测试版中可用,因此请小心使用


我只能为您提供一个示例的Java代码

(对于我所做的一切,我都使用ScribeJava获取身份验证令牌) 对于委派的权限,您需要具有“用户身份验证令牌”。这意味着您必须像下面这样使用密码凭据授权:

private void _initOAuth2Service() 
{
    oAuth2Service = new ServiceBuilder(clientId)
            .apiSecret(clientSecret)
            .defaultScope(GRAPH_SCOPE)
            .build(MicrosoftAzureActiveDirectory20Api.custom(tenantId));


    //PASSWORD CREDENTIALS FLOW
    try 
    {
        oAuth2Token = oAuth2Service.getAccessTokenPasswordGrant(username, password);
    }
    catch (IOException e) { e.printStackTrace();  }
    catch (InterruptedException e) { e.printStackTrace(); }
    catch (ExecutionException e) {  e.printStackTrace(); }
}
{"@odata.context":"https://graph.microsoft.com/beta/$metadata#chats","value":[{"id":"{PartOfTheID}_{firstHalfOfUserID}-e52a55572b49@unq.gbl.spaces","topic":null,"createdDateTime":"2020-04-25T09:22:19.86Z","lastUpdatedDateTime":"2020-04-25T09:22:20.46Z"},{"id":"{secondUserChatID}@unq.gbl.spaces","topic":null,"createdDateTime":"2020-03-27T08:19:29.257Z","lastUpdatedDateTime":"2020-03-27T08:19:30.255Z"}]}
final OAuthRequest request = new OAuthRequest(Verb.POST, "https://graph.microsoft.com/beta/chats/{PartOfTheID}_{firstHalfOfUserID}-e52a55572b49@unq.gbl.spaces/messages");
        oAuth2Service.signRequest(oAuth2Token, request);
        request.addHeader("Accept", "application/json, text/plain, */*");
        request.addHeader("Content-Type", "application/json");
        request.setPayload("{\"body\":{\"content\":\" " + "Hi Hi Daniel Adu-Djan" + "\"}}");
        oAuth2Service.execute(request);
IGraphServiceClient graphUserClient = _initGraphServiceUserClient();
//Set Beta-Endpoint 
graphUserClient.setServiceRoot("https://graph.microsoft.com/beta");
用户名和密码是您要发送消息的用户(发件人)的凭据


初始情况

这是我的团队客户:

ScribeJava


获取所有开放式聊天

try
    {
      JsonObject allChats = graphUserClient.customRequest("/me/chats").buildRequest().get();
    }
catch(ClientException ex) { ex.printStacktrace(); }
(URL中的“我”是上面的用户(发件人)。)

我从该请求得到的响应如下所示:

private void _initOAuth2Service() 
{
    oAuth2Service = new ServiceBuilder(clientId)
            .apiSecret(clientSecret)
            .defaultScope(GRAPH_SCOPE)
            .build(MicrosoftAzureActiveDirectory20Api.custom(tenantId));


    //PASSWORD CREDENTIALS FLOW
    try 
    {
        oAuth2Token = oAuth2Service.getAccessTokenPasswordGrant(username, password);
    }
    catch (IOException e) { e.printStackTrace();  }
    catch (InterruptedException e) { e.printStackTrace(); }
    catch (ExecutionException e) {  e.printStackTrace(); }
}
{"@odata.context":"https://graph.microsoft.com/beta/$metadata#chats","value":[{"id":"{PartOfTheID}_{firstHalfOfUserID}-e52a55572b49@unq.gbl.spaces","topic":null,"createdDateTime":"2020-04-25T09:22:19.86Z","lastUpdatedDateTime":"2020-04-25T09:22:20.46Z"},{"id":"{secondUserChatID}@unq.gbl.spaces","topic":null,"createdDateTime":"2020-03-27T08:19:29.257Z","lastUpdatedDateTime":"2020-03-27T08:19:30.255Z"}]}
final OAuthRequest request = new OAuthRequest(Verb.POST, "https://graph.microsoft.com/beta/chats/{PartOfTheID}_{firstHalfOfUserID}-e52a55572b49@unq.gbl.spaces/messages");
        oAuth2Service.signRequest(oAuth2Token, request);
        request.addHeader("Accept", "application/json, text/plain, */*");
        request.addHeader("Content-Type", "application/json");
        request.setPayload("{\"body\":{\"content\":\" " + "Hi Hi Daniel Adu-Djan" + "\"}}");
        oAuth2Service.execute(request);
IGraphServiceClient graphUserClient = _initGraphServiceUserClient();
//Set Beta-Endpoint 
graphUserClient.setServiceRoot("https://graph.microsoft.com/beta");
您可以看到,我有两个打开的聊天,并从请求中获得两个条目

获取正确的会话ID

您必须知道,id可以分为三个部分。{JustAPartOfTheId}{userId}@{EndOfTheId}。{userId}是您的聊天伙伴(收件人)的id

这是一个GrapherXPlorer响应,它向我提供了所有用户以及关于他们的所有信息

现在您可以看到第一个ID: “id”:“{partofid}{firsthalfofserid}”-e52a55572b49@unq.gbl.spaces"

匹配“\”后面的用户标识

您可以在“u”筛选器中拆分ID并找到所需的ID

向用户发送消息

现在您知道了正确的Id,可以发送新的消息请求,如下所示:

private void _initOAuth2Service() 
{
    oAuth2Service = new ServiceBuilder(clientId)
            .apiSecret(clientSecret)
            .defaultScope(GRAPH_SCOPE)
            .build(MicrosoftAzureActiveDirectory20Api.custom(tenantId));


    //PASSWORD CREDENTIALS FLOW
    try 
    {
        oAuth2Token = oAuth2Service.getAccessTokenPasswordGrant(username, password);
    }
    catch (IOException e) { e.printStackTrace();  }
    catch (InterruptedException e) { e.printStackTrace(); }
    catch (ExecutionException e) {  e.printStackTrace(); }
}
{"@odata.context":"https://graph.microsoft.com/beta/$metadata#chats","value":[{"id":"{PartOfTheID}_{firstHalfOfUserID}-e52a55572b49@unq.gbl.spaces","topic":null,"createdDateTime":"2020-04-25T09:22:19.86Z","lastUpdatedDateTime":"2020-04-25T09:22:20.46Z"},{"id":"{secondUserChatID}@unq.gbl.spaces","topic":null,"createdDateTime":"2020-03-27T08:19:29.257Z","lastUpdatedDateTime":"2020-03-27T08:19:30.255Z"}]}
final OAuthRequest request = new OAuthRequest(Verb.POST, "https://graph.microsoft.com/beta/chats/{PartOfTheID}_{firstHalfOfUserID}-e52a55572b49@unq.gbl.spaces/messages");
        oAuth2Service.signRequest(oAuth2Token, request);
        request.addHeader("Accept", "application/json, text/plain, */*");
        request.addHeader("Content-Type", "application/json");
        request.setPayload("{\"body\":{\"content\":\" " + "Hi Hi Daniel Adu-Djan" + "\"}}");
        oAuth2Service.execute(request);
IGraphServiceClient graphUserClient = _initGraphServiceUserClient();
//Set Beta-Endpoint 
graphUserClient.setServiceRoot("https://graph.microsoft.com/beta");

GraphAPI自定义请求

try
    {
      JsonObject allChats = graphUserClient.customRequest("/me/chats").buildRequest().get();
    }
catch(ClientException ex) { ex.printStacktrace(); }
在Graph SDK中,除了自定义请求外,没有机会使用beta端点。(对于这些请求,我还使用ScribeJava获取身份验证令牌)

设置测试端点

当您想使用BETA端点时,必须使用setEndpoint()函数,如下所示:

private void _initOAuth2Service() 
{
    oAuth2Service = new ServiceBuilder(clientId)
            .apiSecret(clientSecret)
            .defaultScope(GRAPH_SCOPE)
            .build(MicrosoftAzureActiveDirectory20Api.custom(tenantId));


    //PASSWORD CREDENTIALS FLOW
    try 
    {
        oAuth2Token = oAuth2Service.getAccessTokenPasswordGrant(username, password);
    }
    catch (IOException e) { e.printStackTrace();  }
    catch (InterruptedException e) { e.printStackTrace(); }
    catch (ExecutionException e) {  e.printStackTrace(); }
}
{"@odata.context":"https://graph.microsoft.com/beta/$metadata#chats","value":[{"id":"{PartOfTheID}_{firstHalfOfUserID}-e52a55572b49@unq.gbl.spaces","topic":null,"createdDateTime":"2020-04-25T09:22:19.86Z","lastUpdatedDateTime":"2020-04-25T09:22:20.46Z"},{"id":"{secondUserChatID}@unq.gbl.spaces","topic":null,"createdDateTime":"2020-03-27T08:19:29.257Z","lastUpdatedDateTime":"2020-03-27T08:19:30.255Z"}]}
final OAuthRequest request = new OAuthRequest(Verb.POST, "https://graph.microsoft.com/beta/chats/{PartOfTheID}_{firstHalfOfUserID}-e52a55572b49@unq.gbl.spaces/messages");
        oAuth2Service.signRequest(oAuth2Token, request);
        request.addHeader("Accept", "application/json, text/plain, */*");
        request.addHeader("Content-Type", "application/json");
        request.setPayload("{\"body\":{\"content\":\" " + "Hi Hi Daniel Adu-Djan" + "\"}}");
        oAuth2Service.execute(request);
IGraphServiceClient graphUserClient = _initGraphServiceUserClient();
//Set Beta-Endpoint 
graphUserClient.setServiceRoot("https://graph.microsoft.com/beta");
获取所有聊天记录

try
    {
      JsonObject allChats = graphUserClient.customRequest("/me/chats").buildRequest().get();
    }
catch(ClientException ex) { ex.printStacktrace(); }
与上述相同的响应

IGraphServiceClient graphUserClient = _initGraphServiceUserClient();
//Set Beta-Endpoint again
graphUserClient.setServiceRoot("https://graph.microsoft.com/beta"); 
try
    {
      JsonObject allChats = graphUserClient.customRequest("/chats/{PartOfTheID}_{firstHalfOfUserID}-e52a55572b49@unq.gbl.spaces/messages").buildRequest().post({yourMessageAsJsonObject});
    }
catch(ClientException ex) { ex.printStacktrace(); 
}
与userId=>拆分和筛选的情况相同

发送消息

IGraphServiceClient graphUserClient = _initGraphServiceUserClient();
//Set Beta-Endpoint again
graphUserClient.setServiceRoot("https://graph.microsoft.com/beta"); 
try
    {
      JsonObject allChats = graphUserClient.customRequest("/chats/{PartOfTheID}_{firstHalfOfUserID}-e52a55572b49@unq.gbl.spaces/messages").buildRequest().post({yourMessageAsJsonObject});
    }
catch(ClientException ex) { ex.printStacktrace(); 
}

这里有一个小GIF,你可以看到我没有键入任何内容。我刚刚启动了我的小应用程序,它会自动发送消息

我希望这对你有帮助。如果您不理解某些内容,请随时发表评论!:)

致以最良好的祝愿

(由于清晰和添加了自定义请求而编辑)

您可以通过Graph API向私人用户发送消息,但存在一个问题,即无法通过Graph API在两个用户之间创建新的聊天。这意味着,如果要从用户向用户发送消息,则聊天必须已经存在。(必须先通过MSTeams客户端交换消息,才能进行聊天)

所以,确保你有一个开放的聊天

如果是,请查看此MSDoc(此文档说明如何列出用户的聊天记录):

列出所有聊天记录后,您可以查看此MSDoc(本文档说明如何向用户发送消息):

注意权限!对于发送消息和列表聊天,到目前为止只有委派权限这些呼叫仅在测试版中可用,因此请小心使用


我只能为您提供一个示例的Java代码

(对于我所做的一切,我都使用ScribeJava获取身份验证令牌) 对于委派的权限,您需要具有“用户身份验证令牌”。这意味着您必须像下面这样使用密码凭据授权:

private void _initOAuth2Service() 
{
    oAuth2Service = new ServiceBuilder(clientId)
            .apiSecret(clientSecret)
            .defaultScope(GRAPH_SCOPE)
            .build(MicrosoftAzureActiveDirectory20Api.custom(tenantId));


    //PASSWORD CREDENTIALS FLOW
    try 
    {
        oAuth2Token = oAuth2Service.getAccessTokenPasswordGrant(username, password);
    }
    catch (IOException e) { e.printStackTrace();  }
    catch (InterruptedException e) { e.printStackTrace(); }
    catch (ExecutionException e) {  e.printStackTrace(); }
}
{"@odata.context":"https://graph.microsoft.com/beta/$metadata#chats","value":[{"id":"{PartOfTheID}_{firstHalfOfUserID}-e52a55572b49@unq.gbl.spaces","topic":null,"createdDateTime":"2020-04-25T09:22:19.86Z","lastUpdatedDateTime":"2020-04-25T09:22:20.46Z"},{"id":"{secondUserChatID}@unq.gbl.spaces","topic":null,"createdDateTime":"2020-03-27T08:19:29.257Z","lastUpdatedDateTime":"2020-03-27T08:19:30.255Z"}]}
final OAuthRequest request = new OAuthRequest(Verb.POST, "https://graph.microsoft.com/beta/chats/{PartOfTheID}_{firstHalfOfUserID}-e52a55572b49@unq.gbl.spaces/messages");
        oAuth2Service.signRequest(oAuth2Token, request);
        request.addHeader("Accept", "application/json, text/plain, */*");
        request.addHeader("Content-Type", "application/json");
        request.setPayload("{\"body\":{\"content\":\" " + "Hi Hi Daniel Adu-Djan" + "\"}}");
        oAuth2Service.execute(request);
IGraphServiceClient graphUserClient = _initGraphServiceUserClient();
//Set Beta-Endpoint 
graphUserClient.setServiceRoot("https://graph.microsoft.com/beta");
用户名和密码是您要发送消息的用户(发件人)的凭据


初始情况

这是我的团队客户:

ScribeJava


获取所有开放式聊天

try
    {
      JsonObject allChats = graphUserClient.customRequest("/me/chats").buildRequest().get();
    }
catch(ClientException ex) { ex.printStacktrace(); }
(URL中的“我”是上面的用户(发件人)。)

我从该请求得到的响应如下所示:

private void _initOAuth2Service() 
{
    oAuth2Service = new ServiceBuilder(clientId)
            .apiSecret(clientSecret)
            .defaultScope(GRAPH_SCOPE)
            .build(MicrosoftAzureActiveDirectory20Api.custom(tenantId));


    //PASSWORD CREDENTIALS FLOW
    try 
    {
        oAuth2Token = oAuth2Service.getAccessTokenPasswordGrant(username, password);
    }
    catch (IOException e) { e.printStackTrace();  }
    catch (InterruptedException e) { e.printStackTrace(); }
    catch (ExecutionException e) {  e.printStackTrace(); }
}
{"@odata.context":"https://graph.microsoft.com/beta/$metadata#chats","value":[{"id":"{PartOfTheID}_{firstHalfOfUserID}-e52a55572b49@unq.gbl.spaces","topic":null,"createdDateTime":"2020-04-25T09:22:19.86Z","lastUpdatedDateTime":"2020-04-25T09:22:20.46Z"},{"id":"{secondUserChatID}@unq.gbl.spaces","topic":null,"createdDateTime":"2020-03-27T08:19:29.257Z","lastUpdatedDateTime":"2020-03-27T08:19:30.255Z"}]}
final OAuthRequest request = new OAuthRequest(Verb.POST, "https://graph.microsoft.com/beta/chats/{PartOfTheID}_{firstHalfOfUserID}-e52a55572b49@unq.gbl.spaces/messages");
        oAuth2Service.signRequest(oAuth2Token, request);
        request.addHeader("Accept", "application/json, text/plain, */*");
        request.addHeader("Content-Type", "application/json");
        request.setPayload("{\"body\":{\"content\":\" " + "Hi Hi Daniel Adu-Djan" + "\"}}");
        oAuth2Service.execute(request);
IGraphServiceClient graphUserClient = _initGraphServiceUserClient();
//Set Beta-Endpoint 
graphUserClient.setServiceRoot("https://graph.microsoft.com/beta");
您可以看到,我有两个打开的聊天,并从请求中获得两个条目

获取正确的会话ID

您必须知道,id可以分为三个部分。{JustAPartOfTheId}{userId}@{EndOfTheId}。{userId}是您的聊天伙伴(收件人)的id

这是一个GrapherXPlorer响应,它向我提供了所有用户以及关于他们的所有信息

现在您可以看到第一个ID: “id”:“{partofid}{firsthalfofserid}”-e52a55572b49@unq.gbl.spaces"

匹配“\”后面的用户标识

您可以在“u”筛选器中拆分ID并找到所需的ID

向用户发送消息

现在您知道了正确的Id,可以发送新的消息请求,如下所示:

private void _initOAuth2Service() 
{
    oAuth2Service = new ServiceBuilder(clientId)
            .apiSecret(clientSecret)
            .defaultScope(GRAPH_SCOPE)
            .build(MicrosoftAzureActiveDirectory20Api.custom(tenantId));


    //PASSWORD CREDENTIALS FLOW
    try 
    {
        oAuth2Token = oAuth2Service.getAccessTokenPasswordGrant(username, password);
    }
    catch (IOException e) { e.printStackTrace();  }
    catch (InterruptedException e) { e.printStackTrace(); }
    catch (ExecutionException e) {  e.printStackTrace(); }
}
{"@odata.context":"https://graph.microsoft.com/beta/$metadata#chats","value":[{"id":"{PartOfTheID}_{firstHalfOfUserID}-e52a55572b49@unq.gbl.spaces","topic":null,"createdDateTime":"2020-04-25T09:22:19.86Z","lastUpdatedDateTime":"2020-04-25T09:22:20.46Z"},{"id":"{secondUserChatID}@unq.gbl.spaces","topic":null,"createdDateTime":"2020-03-27T08:19:29.257Z","lastUpdatedDateTime":"2020-03-27T08:19:30.255Z"}]}
final OAuthRequest request = new OAuthRequest(Verb.POST, "https://graph.microsoft.com/beta/chats/{PartOfTheID}_{firstHalfOfUserID}-e52a55572b49@unq.gbl.spaces/messages");
        oAuth2Service.signRequest(oAuth2Token, request);
        request.addHeader("Accept", "application/json, text/plain, */*");
        request.addHeader("Content-Type", "application/json");
        request.setPayload("{\"body\":{\"content\":\" " + "Hi Hi Daniel Adu-Djan" + "\"}}");
        oAuth2Service.execute(request);
IGraphServiceClient graphUserClient = _initGraphServiceUserClient();
//Set Beta-Endpoint 
graphUserClient.setServiceRoot("https://graph.microsoft.com/beta");

GraphAPI自定义请求

try
    {
      JsonObject allChats = graphUserClient.customRequest("/me/chats").buildRequest().get();
    }
catch(ClientException ex) { ex.printStacktrace(); }
在Graph SDK中,除了自定义请求外,没有机会使用beta端点。(对于这些请求,我还使用ScribeJava获取身份验证令牌)

设置测试端点

当您想要使用BETA端点时,您必须使用se