question

SaravanakumarP-9122 avatar image
0 Votes"
SaravanakumarP-9122 asked SaravanakumarP-9122 commented

How to upload a file to OneDrive using REST API using a C# Application

Hi Team,

We are trying to upload a file to OneDrive using REST API using a C# Application, but we are unable to create an Authentication token that is required for submitting the file upload. We have tried one drive functionality using documentation and some reference links we got from google search but nothing seems to be working.

Thank you.

dotnet-csharp
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Daniel,

We followed the provided link already but we are getting the Invalid client secret.

Thak you,
Saravankumar P

0 Votes 0 ·

Hi @ravanakumarP-9122,
What is the specific error message?
And make sure you have copied client secret value and not the client secret ID.
Best Regards,
Daniel Zhang

0 Votes 0 ·

Hi Daniel,

We have successfully generated the access token, but when we uploading the file as given in the link we are getting the below "api_deprecated" error.

Please find the below screenshot,


105754-reply-3.png


0 Votes 0 ·
reply-3.png (76.7 KiB)

Hi Daniel,

Can you please provide some code samples on how to upload files to one drive using Microsoft Graph?

Thanks,
Saravanakumar P

0 Votes 0 ·

Hi @SaravanakumarP-9122,
You can try another way to upload a file to OneDrive is using the PUT method in this thread that kiewic provided.
Best Regards,
Daniel Zhang


0 Votes 0 ·
DanielZhang-MSFT avatar image
0 Votes"
DanielZhang-MSFT answered

Hi SaravanakumarP-9122,
Have you tried the following documents? And what specific problems did you encounter?
Downloading and uploading files on OneDrive (REST)
Upload and Download files from OneDrive
Here are also some related threads you can refer to.
Upload file (> 4MB) to OneDrive using Graph API
how to upload and download file to onedrive for business using c# application?
Create file to Onedrive programmatically from C#?
Best Regards,
Daniel Zhang


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SaravanakumarP-9122 avatar image
0 Votes"
SaravanakumarP-9122 answered SaravanakumarP-9122 edited

Hi Daniel,

Link: Upload file (> 4MB) to OneDrive using Graph API



private string GetAccessToken()
{
string accessToken;
string CLIENT_ID = "<YOUR_CLIENT_ID>";
string CLIENT_SECRET = "<YOUR_CLIENT_SECRET>";
string REFRESH_TOKEN = "<YOUR_REFRESH_TOKEN>";
string REDIRECT_URL = "<YOUR_REDIRECT_URL>";

string param = "client_id=" + CLIENT_ID + "&redirect_uri=" + REDIRECT_URL + "&client_secret=" + CLIENT_SECRET + "&refresh_token=" + REFRESH_TOKEN + "&grant_type=refresh_token";

var client = new RestClient("https://login.microsoftonline.com/common/oauth2/v2.0/token");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", param, ParameterType.RequestBody);
var response = client.Execute(request);
try
{
var json = JObject.Parse(response.Content);

if (!string.IsNullOrEmpty(Convert.ToString(json["error_description"])))
{
HttpContext.Current.Response.Redirect("/OneDriveLoginPage.aspx?redirect=" + HttpContext.Current.Request.RawUrl);
}
accessToken = Convert.ToString(json["access_token"]);
string refreshToken = Convert.ToString(json["refresh_token"]);

//keep your refresh token saved somewhere
}
catch (Exception ex)
{
throw ex;
}

return accessToken;
}

How to get this <YOUR_REFRESH_TOKEN>

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SaravanakumarP-9122 avatar image
0 Votes"
SaravanakumarP-9122 answered DanielZhang-MSFT commented

Link: Downloading and uploading files on OneDrive (REST)

Getting error on -> Signing users in with REST -> Get an access token and an authentication token

Registered App in Azure Portal => App Registration, Got Client ID on registration and created a client secret also using the provided option.

But it says invalid client_secret - Please find the attached image

105037-image.png



image.png (96.3 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @SaravanakumarP-9122,
Please follow this document to get a valid client secret.
Best Regards,
Daniel Zhang


0 Votes 0 ·
SaravanakumarP-9122 avatar image
0 Votes"
SaravanakumarP-9122 answered SaravanakumarP-9122 commented

Hi Daniel,

I'm trying to uploading a file to one drive using a C# console application. When I try to request an access token, I'm getting a sign-in page as a response. here is the below code

string url = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=myclientidhere&scope=files.readwrite.all&response_type=token&redirect_uri=http://localhost";

WebRequest request = WebRequest.Create(url);

request.Method = "GET";

using (WebResponse response = request.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream);
string text = reader.ReadToEnd();
}
}


Reference Link: https://docs.microsoft.com/en-us/onedrive/developer/rest-api/getting-started/graph-oauth?view=odsp-graph-online

Thank you,
Saravanakumar P



· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @SaravanakumarP-9122,
Based on your code, what specific problem did you encounter?
Best Regards,
Daniel Zhang

0 Votes 0 ·

Hi Daniel,

The response should be of http://localhost, but I'm getting Microsoft sign-in page as a response.

Thank you,
Saravanakumar P

0 Votes 0 ·