使用Microsoft Graph API将文件上传到MVC应用程序中的onedrive,权限错误

问题描述 投票:1回答:1

我正在尝试将文件上传到用户的单一驱动器。我正在尝试使用c#SDK上载文件。但我收到一条错误消息:

代码:accessDenied

消息:呼叫者没有执行该操作的权限。

有人可以帮助我解决这些权限问题吗?

我已在Azure中设置我的应用程序,已将用户添加到该应用程序中。我添加了Microsoft Graph API权限:委派权限:User.Read Files.Read Files.Read.All Files.ReadWrite.All Sites.Read.All Sites.ReadWrite.All offline_access openid配置文件

这是我用于通过Graph SDK登录的代码:

  GraphServiceClient client = new GraphServiceClient(new  GraphAuthenticationProvider());

这是AuthenticationProvider

  public class GraphAuthenticationProvider : IAuthenticationProvider
{
    AzureTokenResponse tokenRes;

    public async Task AuthenticateRequestAsync(HttpRequestMessage request)
    {
        try
        {
            if (tokenRes == null || tokenRes.ExpirationDate < DateTime.Now)
            {
                string tokenEndpointUri = "https://login.microsoftonline.com/" + "{domain}" + "/oauth2/token";

                var content = new FormUrlEncodedContent(new[]
                {
             new KeyValuePair<string, string>("username", "{username}"),
             new KeyValuePair<string, string>("password", "{password}"),
             new KeyValuePair<string, string>("client_id", "{clientid}"),
             new KeyValuePair<string, string>("client_secret", "{clientsecret}"),
             new KeyValuePair<string, string>("grant_type", "password"),
             new KeyValuePair<string,string>("scope","User.Read Files.Read Files.ReadWrite Files.ReadWrite.All Sites.Read.All Sites.ReadWrite.All"),
             new KeyValuePair<string, string>("resource", "https://graph.microsoft.com")
            });

                using (var client = new HttpClient())
                {
                    HttpResponseMessage res = await client.PostAsync(tokenEndpointUri, content);

                    string json = await res.Content.ReadAsStringAsync();

                    tokenRes = JsonConvert.DeserializeObject<AzureTokenResponse>(json);

                    tokenRes.ExpirationDate = DateTime.Parse("1970/01/01");

                    int seconds = 0;

                    int.TryParse(tokenRes.Expires_on, out seconds);

                    tokenRes.ExpirationDate = TimeZone.CurrentTimeZone.ToLocalTime(tokenRes.ExpirationDate.AddSeconds(seconds));
                }
            }
            else
            {

            }
            request.Headers.Add("Authorization", "Bearer " + tokenRes.AccessToken);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }
    }

    class AzureTokenResponse
    {
        [JsonProperty("access_token")]
        public string AccessToken { get; set; }

        [JsonProperty("expires_in")]
        public string Expires_in { get; set; }

        [JsonProperty("expires_on")]
        public string Expires_on { get; set; }

        public DateTime ExpirationDate { get; set; }
    }

}

这是我要拨打的电话:上传文件:

   string path = Path.GetTempFileName();

        file.SaveAs(path);

        byte[] data = System.IO.File.ReadAllBytes(path);

        Stream filestream = new MemoryStream(data);

        DriveItem doc = await client.Me.Drive.Root.ItemWithPath(file.FileName).Content.Request().PutAsync<DriveItem>(filestream);

获取文件链接:

   Microsoft.Graph.Permission permission = await client.Me.Drive.Items[doc.Id].CreateLink("embed", "anonymous").Request().PostAsync();

我获得了访问令牌,但是当我尝试使用它来上传文件时,出现错误,提示我缺少权限

c# asp.net-mvc microsoft-graph onedrive microsoft-graph-sdks
1个回答
0
投票

我一直在或多或少地为同一个主题苦苦挣扎。据我所知,Graph仅在OneDrive for Business(委派)或SharePoint(委派和应用程序权限)上支持文件操作(读/写)。

© www.soinside.com 2019 - 2024. All rights reserved.