Hi @Rohit Devmore ,
According to my research and testing, If I use Client-Id and Client-Secret, I will get the same error as you, but if I use an account and password , the above code works well and generates a share link. Therefore, this code does not appear to apply to Client-Id and Client-Secret.
As a workaround, you can try to use the following code to create external sharing link:
class Program
{
static void Main(string[] args)
{
string siteUrl = "https://xxxxx.sharepoint.com/sites/zellatest";
string clientId = "xxxxxxxxxxxxx";
string clientSecret = "xxxxxxxxxxx";
//For SharePoint app only auth, the scope will be the SharePoint tenant name followed by /.default
var scopes = new string[] { "https://xxxxx.sharepoint.com/sites/zellatest" };
using (var cc = new PnP.Framework.AuthenticationManager().GetACSAppOnlyContext(siteUrl, clientId, clientSecret))
{
cc.Load(cc.Web, p => p.Title);
cc.ExecuteQuery();
Debug.WriteLine(cc.Web.Title);
var web = cc.Web;
var dig = cc.GetFormDigestDirect();
cc.Load(web);
cc.ExecuteQuery();
string link = cc.Web.CreateAnonymousLinkForDocument("https://xxxx.sharepoint.com/sites/zellatest/test/Document.docx", ExternalSharingDocumentOption.View);
Debug.WriteLine(link);
};
}//main
}
My test result:
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
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.