I want to access a Sharepoint 2013 / office365 - site from a console application using OAuth-token I saved earlier using a provider hosted app.

I already can connect using credentials:

using (ClientContext context = new ClientContext("https://mypage.sharepoint.com))
  context.AuthenticationMode = ClientAuthenticationMode.Default;
  context.Credentials = new SharePointOnlineCredentials(_username, secureString);
  return context;

But instead of using _username, secureString (=password) I want to use the saved Token. How can this be done?


Just regenerate the token.

Add the App For SharePoint nuget package to your console app.

Install-Package AppForSharePointWebToolkit

Then write the code to generate the token and context object.

Uri siteUri = new Uri(siteUrl);
string realm = TokenHelper.GetRealmFromTargetUrl(siteUri); string accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, siteUri.Authority, realm).AccessToken;

ClientContext ctx = TokenHelper.GetClientContextWithAccessToken(siteUri.ToString(), accessToken);

You'll also need to put the Client ID and secret into your console app's app.config.

  • Thanks. can the clientId and ClientSecret from the console Application be the same as the application that stores the credentials?
    – Ole Albers
    Apr 5 '16 at 15:02
  • 1
    Yes. Try it and see. Apr 5 '16 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.