繁体   English   中英

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

[英]Using Microsoft Graph API to upload file to onedrive in MVC Application, Error with permissions

我正在尝试将文件上传到用户的 onedrive。 我正在尝试使用 c# SDK 上传文件。 但我收到一个错误说:

代码:访问拒绝

消息:调用者无权执行该操作。

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

我已经在 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();

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM