'Uploading file to SharePoint Online site folder using Microsoft Graph API and MSAL Authentication
I have a WPF/C# application and I am trying to upload files to a SharePoint site's subfolder.
I have followed this example to first register my application in Azure Active Directory. This step is needed since I want to use Microsoft's identity service in my application and avoid having the user enter username and password each time. Instead, Microsoft will return an access token that I can then use to query the Graph API and then SharePoint Online.
I registered the application with the following settings:
- Supported Account Types: Accounts in any organization directory (Any Azure AD directory - Multitenant) and personal Microsoft accounts (e.g. Skype, Xbox)
- Authentication: Mobile and Desktop Applications
- Redirect URIs: https://login.microsoftonline.com/common/oauth2/nativeclient
In addition, I set the API Permissions like so: screenshot
In my code, I added the MSAL package and Microsoft Graph package. Below is the code for acquiring an access token (I followed from the same Microsoft Doc post I linked above).
Here is how I am getting my authorization token:
var authResult = await PublicClientApp.AcquireTokenInteractive(scopes)
.WithAccount(accounts.FirstOrDefault())
.WithPrompt(Microsoft.Identity.Client.Prompt.SelectAccount)
.ExecuteAsync();
Where scopes is:
string[] scopes = new string[] { "user.read" };
Then, after acquiring the authentication, I attempt to use get the SharePoint site's Id and drive Id's as demonstrated in this example.
string site = "mytenant.sharepoint.com";
string relativePath = "/sites/mytargetsite";
if (authResult != null)
{
var authProvider = new DelegateAuthenticationProvider(async (request) =>
{
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", authResult.AccessToken);
});
GraphServiceClient graphClient = new GraphServiceClient(authProvider);
var s = await graphClient
.Sites[this.site]
.SiteWithPath(this.relativePath)
.Request()
.GetAsync();
var d = await graphClient
.Sites[s.Id]
.Drive
.Request()
.GetAsync();
}
There is more to the code to upload a file, but at this point is where my code fails. The error I get is:
"Microsoft.Graph.ServiceException: 'Code: invalidRequest Message: Invalid hostname for this tenancy..."
This is where I am stuck. I am reaching out to see if there is anyone with clues as to what I am doing wrong. Thank you!
Solution 1:[1]
If you already have the token then you add it to the header. Then you make the call with the client like this, this works for send xml files:
string path = $"{appSettings.UrlGraph}sites/{appSettings.SiteId}/drive/items/";
string requestUrl = $"{path}root:/{file.xml}:/content";
StreamContent requestContent = new StreamContent(new MemoryStream(Encoding.UTF8.GetBytes(content)));
requestContent.Headers.ContentType = new MediaTypeHeaderValue("application/xml");
HttpResponseMessage response = await _httpClient.PutAsync(requestUrl, requestContent);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | sergiokml |
