2

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?

1 Answer 1

5

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.

2
  • Thanks. can the clientId and ClientSecret from the console Application be the same as the application that stores the credentials? Commented Apr 5, 2016 at 15:02
  • 1
    Yes. Try it and see. Commented Apr 5, 2016 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.