I'm trying to upload files to the onedrive of a user. I am trying to use the c# SDK to upload the files. But I get an Error saying :
Code: accessDenied
Message: The caller does not have permission to perform the action.
Can someone help me resolve these permission issues?
I have set up my application in Azure, I have added the user to the app. I have Added Microsoft Graph API Permissions: Delegated permissions : User.Read Files.Read Files.Read.All Files.ReadWrite.All Sites.Read.All Sites.ReadWrite.All offline_access openid profile
This is the Code i Use to Sign in with the Graph SDK:
GraphServiceClient client = new GraphServiceClient(new GraphAuthenticationProvider());
This is the 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; }
}
}
This is the calls i am trying to make: Upload File:
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);
Get Link to file:
Microsoft.Graph.Permission permission = await client.Me.Drive.Items[doc.Id].CreateLink("embed", "anonymous").Request().PostAsync();
I get the access token, but when I try to use it to upload files, I get the Error saying I lack permissions
I have been struggling with more or less the same topic. As far as I can figure out Graph only supports File Actions (read/write) on OneDrive for Business (Delegated) or SharePoint (Delegated and Application Permissions).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.