是否可以使用Microsoft-Graph C#客户端库删除消息?

时间:2018-09-29 08:11:45

标签: c# .net outlook office365 microsoft-graph

我在C#.net项目中使用Microsoft Graph客户端,并且我注意到Message项没有公开任何删除消息的方法。

我尝试了

client.Me.Messages[mail.Id].Delete()

我看到Delete方法不存在。

我该怎么办?

3 个答案:

答案 0 :(得分:2)

您需要创建包含Request方法的DeleteAsync()对象:

graphServiceClient
    .Me
    .Messages["your message id"]
    .Request()
    .DeleteAsync();

根据您自己的代码:

await client
  .Me
  .Messages[mail.Id]
  .Request()
  .DeleteAsync();

答案 1 :(得分:0)

我没有使用过此客户端库,但我浏览了its Github repo。从回购文档中尚不清楚如何删除邮件,但是MS Graph的文档表明有一种方法可以通过其API删除邮件:

https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/message_delete

不过,基于客户端存储库,您似乎只需要创建一个MessageRequest并调用其unsigned int offet = 0; for(unsigned int num_meshes=0; num_meshes<scene->mNumMeshes; num_meshes++) { // lets store all the vertices. for(unsigned int num_vertices_per_mesh=0; num_vertices_per_mesh<scene->mMeshes[num_meshes]->mNumVertices; num_vertices_per_mesh++) { glm::vec3 vertex(scene->mMeshes[num_meshes]->mVertices[num_vertices_per_mesh].x, scene->mMeshes[num_meshes]->mVertices[num_vertices_per_mesh].y, scene->mMeshes[num_meshes]->mVertices[num_vertices_per_mesh].z); vertices.push_back(vertex); } // lets store all the indices or faces. for(unsigned int num_faces=0; num_faces<scene->mMeshes[num_meshes]->mNumFaces; num_faces++){ indices.push_back(scene->mMeshes[num_meshes]->mFaces[num_faces].mIndices[0]+offet ); indices.push_back(scene->mMeshes[num_meshes]->mFaces[num_faces].mIndices[1]+offset); indices.push_back(scene->mMeshes[num_meshes]->mFaces[num_faces].mIndices[2]+offset); } offset += num_vertices_per_mesh; } 方法即可。

DeleteAsync()类接受一个请求URL,该URL可能是消息URL(类似MessageRequest之类)。

答案 2 :(得分:-1)

我用了这个:

public static async Task DeleteEmail(string emailId)
        {
            TokenCache tokens = TokenCacheHelper.GetUserCache();
            PublicClientApplication clientApp = new PublicClientApplication(secret, RootAuthUri, tokens);

            using (HttpClient c = new HttpClient())
            {
                string requestURI = RootUri + "/me/messages/" + emailId;

                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Delete, requestURI);
                //Authentication token
                request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", await GetTokenAsync(clientApp));

                HttpResponseMessage response = await c.SendAsync(request);
                string responseString = await response.Content.ReadAsStringAsync();
            }

        }


private static async Task<string> GetTokenAsync(PublicClientApplication app)
        {
            AuthenticationResult result = null;
            string[] scopes = { "User.Read", "Mail.ReadWrite" };
            try
            {

                // Get the token from the cache.
                result = await app.AcquireTokenSilentAsync(scopes, (await app.GetAccountsAsync()).FirstOrDefault());
                return result.AccessToken;
            }
            catch (MsalUiRequiredException ex)
            {
                // A MsalUiRequiredException happened on AcquireTokenSilentAsync. 
                // This indicates you need to call AcquireTokenAsync to acquire a token
                Debug.WriteLine($"MsalUiRequiredException: {ex.Message}");

                try
                {
                    // Dialog opens for user.
                    result = await app.AcquireTokenAsync(scopes);
                    return result.AccessToken;
                }
                catch (MsalException msalex)
                {
                    Debug.WriteLine($"Error Acquiring Token:{System.Environment.NewLine}{msalex}");
                    return null;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Error Acquiring Token Silently:{System.Environment.NewLine}{ex}");
                return null;
            }
        }

static class TokenCacheHelper
    {

        /// <summary>
        /// Get the user token cache
        /// </summary>
        /// <returns></returns>
        public static TokenCache GetUserCache()
        {
            if (usertokenCache == null)
            {
                usertokenCache = new TokenCache();
                usertokenCache.SetBeforeAccess(BeforeAccessNotification);
                usertokenCache.SetAfterAccess(AfterAccessNotification);
            }
            return usertokenCache;
        }

        static TokenCache usertokenCache;

        /// <summary>
        /// Path to the token cache
        /// </summary>
        public static readonly string CacheFilePath = System.Reflection.Assembly.GetExecutingAssembly().Location + ".msalcache.bin";

        private static readonly object FileLock = new object();

        public static void BeforeAccessNotification(TokenCacheNotificationArgs args)
        {

            lock (FileLock)
            {
                args.TokenCache.Deserialize(File.Exists(CacheFilePath)
                    ? ProtectedData.Unprotect(File.ReadAllBytes(CacheFilePath),
                                              null,
                                              DataProtectionScope.CurrentUser)
                    : null);
            }
        }

        public static void AfterAccessNotification(TokenCacheNotificationArgs args)
        {
            // if the access operation resulted in a cache update
            if (args.TokenCache.HasStateChanged)
            {
                lock (FileLock)
                {
                    // reflect changesgs in the persistent store
                    File.WriteAllBytes(CacheFilePath,
                                       ProtectedData.Protect(args.TokenCache.Serialize(),
                                                             null,
                                                             DataProtectionScope.CurrentUser)
                                      );
                    // once the write operationtakes place restore the HasStateChanged bit to filse
                    args.TokenCache.HasStateChanged = false;
                }
            }
        }
    }