简体   繁体   English

具有令牌的C#CSOM共享点访问

[英]C# CSOM Sharepoint Access with Tokens

I have been really struggling with getting Access Tokens working with our Sharepoint site and still no luck. 我一直在努力让Access Tokens与我们的Sharepoint网站一起工作,但仍然没有运气。

The problem is when I access our site using the SharepointOnlineCredentials it works fine, well I can query the sharepoint as follows and this works but I want to use the new token stuff that just doesn't work for me 问题是,当我使用SharepointOnlineCredentials访问我们的网站时,它工作正常,很好,我可以按以下方式查询共享点,并且此方法有效,但我想使用对我不起作用的新令牌内容

using (ClientContext context = new ClientContext(sharepointSiteUrl))
{
    context.Credentials = GetCredentials();
    var web = context.Web;
    context.Load(web, w => w.Title);
    context.ExecuteQuery();
    if (context.Web.IsPropertyAvailable("Title"))
    {
        Console.WriteLine("Found title");
    }
    Console.WriteLine("Title: {0}", web.Title);
}

private static SharePointOnlineCredentials GetCredentials()
{
    SecureString password = new SecureString();
    foreach (char c in "MyPassword".ToCharArray()) password.AppendChar(c);
    return new SharePointOnlineCredentials("myname@mycompany.com", password);
}

So now for the code that doesn't work 现在,对于无效的代码

authContext = new AuthenticationContext(authority, new FileCache());

ClientCredential clientCredentials = new ClientCredential(clientId, clientSecret);
AuthenticationResult authenticationResult = authContext.AcquireToken(resource, clientCredentials);
Console.WriteLine("Token Type: {0} \nExpires: {1} \nToken: {2}", authenticationResult.AccessTokenType, authenticationResult.ExpiresOn, authenticationResult.AccessToken);

using (ClientContext context = TokenHelper.GetClientContextWithAccessToken(sharepointSiteUrl, authenticationResult.AccessToken))
{
    var web = context.Web;
    context.Load(web);
    context.ExecuteQuery();
    if (context.Web.IsPropertyAvailable("Title"))
    {
        Console.WriteLine("Found title");
    }
    Console.WriteLine("Title: {0}", web.Title);
}

Now when I run this the web.Title throws an error and the context.Web display ' Cannot find the method on the object instance. 现在,当我运行此命令时,web.Title会引发错误并显示上下文。Web显示“ 无法在对象实例上找到方法。 '

The web.Title in the debugger also states ' The property or field 'Title' has not been initialized. 调试器中的web.Title也指出' 属性或字段'Title'尚未初始化。 It has not been requested or the request has not been executed. 尚未请求或尚未执行请求。 It may need to be explicitly requested .' 可能需要明确要求它

I really would love some help with this as it's turned impossible for me. 我真的很乐意提供一些帮助,因为这对我来说是不可能的。 Thanks :) 谢谢 :)

Do I need to be aware or granting access somewhere else? 我是否需要了解或授予其他地方的访问权限? as its strange that the first one works! 奇怪的是第一个起作用了!

In the first example you are explicitly requesting the Title property from the CSOM end-point: 在第一个示例中,您要从CSOM端点显式请求Title属性:

context.Credentials = GetCredentials();
var web = context.Web;
context.Load(web, w => w.Title); // explicitly requesting the Title property
context.ExecuteQuery();

Whereas in the second example there is no context.Load(web, w => w.Title); 而在第二个示例中没有context.Load(web, w => w.Title);

Maybe this is the issue? 也许这是问题所在?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM