C# 在MVC应用程序中使用Microsoft Graph API将文件上载到onedrive,权限错误
我正在尝试将文件上载到用户的onedrive。我正在尝试使用c#SDK上传文件。但我得到一个错误,说: 代码:访问被拒绝 消息:调用方没有执行该操作的权限 有人能帮我解决这些权限问题吗 我已经在Azure中设置了我的应用程序,我已经将用户添加到应用程序中。我已添加Microsoft Graph API权限: 委派权限:User.Read Files.Read Files.Read.All Files.ReadWrite.All Sites.Read.All Sites.ReadWrite.All offline\u访问openid配置文件 这是我用来登录Graph SDK的代码:C# 在MVC应用程序中使用Microsoft Graph API将文件上载到onedrive,权限错误,c#,asp.net-mvc,microsoft-graph-api,onedrive,microsoft-graph-sdks,C#,Asp.net Mvc,Microsoft Graph Api,Onedrive,Microsoft Graph Sdks,我正在尝试将文件上载到用户的onedrive。我正在尝试使用c#SDK上传文件。但我得到一个错误,说: 代码:访问被拒绝 消息:调用方没有执行该操作的权限 有人能帮我解决这些权限问题吗 我已经在Azure中设置了我的应用程序,我已经将用户添加到应用程序中。我已添加Microsoft Graph API权限: 委派权限:User.Read Files.Read Files.Read.All Files.ReadWrite.All Sites.Read.All Sites.ReadWrite.All
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; }
}
}
我获得了访问令牌,但当我尝试使用它上载文件时,我得到一个错误,说我没有权限我一直在或多或少地为同一主题而挣扎。据我所知,Graph仅支持OneDrive for Business(委托)或SharePoint(委托和应用程序权限)上的文件操作(读/写)。我一直在或多或少地为同一主题而挣扎。据我所知,Graph仅支持OneDrive for Business(委派)或SharePoint(委派和应用程序权限)上的文件操作(读/写)。是否有原因不能使用MSAL库进行身份验证?是否有原因不能使用MSAL库进行身份验证?
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();