【问题标题】:Using Microsoft Graph API to upload file to onedrive in MVC Application, Error with permissions使用 Microsoft Graph API 将文件上传到 MVC 应用程序中的 onedrive,权限错误
【发布时间】:2019-06-11 12:04:13
【问题描述】:

我正在尝试将文件上传到用户的 onedrive。我正在尝试使用 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 profile

这是我用来登录 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();

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

【问题讨论】:

  • 是否有不能使用 MSAL 库进行身份验证的原因?

标签: c# asp.net-mvc microsoft-graph-api onedrive microsoft-graph-sdks


【解决方案1】:

我一直在努力解决或多或少相同的话题。据我所知,Graph 仅支持 OneDrive for Business(委托)或 SharePoint(委托和应用程序权限)上的文件操作(读/写)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-30
    • 1970-01-01
    相关资源
    最近更新 更多