Share via

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

Saravanakumar P 6 Reputation points
9 Jun 2021, 5:33 am

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.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,159 questions
{count} vote

4 answers

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,631 Reputation points
    10 Jun 2021, 3:22 am

    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.

    0 comments No comments

  2. Saravanakumar P 6 Reputation points
    11 Jun 2021, 11:12 am

    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>

    0 comments No comments

  3. Saravanakumar P 6 Reputation points
    12 Jun 2021, 3:55 pm

    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


  4. Saravanakumar P 6 Reputation points
    14 Jul 2021, 5:36 am

    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://learn.microsoft.com/en-us/onedrive/developer/rest-api/getting-started/graph-oauth?view=odsp-graph-online

    Thank you,
    Saravanakumar P


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.