Mail Merge Word Document in C# .NET with Aspose.Words REST API throwing 401 unauthorized error

Hi!

I have downloaded Aspose.Words for Cloud - .NET SDK Examples from:
_https://github.com/aspose-words/Aspose.Words-for-Cloud/tree/master/Examples/DotNET_

and want to test ExecuteTemplate.Run();

I have entered the APP SID and APP KEY and uploaded TestExecuteTemplate.doc and TestExecuteTemplateData.txt to my Aspose Cloud account.

When I run RunExamples.cs I get an error:

at Mail_Merge.ExecuteTemplate.Run() in C:\Users\Softwareselskabet\Desktop\Aspose.Words-for-Cloud-master\Examples\DotNET\CSharp\Mail-Merge\ExecuteTemplate.cs:line 28
Exception thrown: ‘Com.Aspose.Storage.ApiException’ in Aspose.Storage-Cloud.dll
error:The remote server returned an error: (401) Unauthorized.

Could someone help me solve the problem?

Complete log:
‘Aspose.Words.Cloud.Examples.CSharp.exe’ (CLR v4.0.30319: DefaultDomain): Loaded ‘C:\Windows\MicrosoftNet\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll’. Skipped loading symbols. Module is optimized and the debugger option ‘Just My Code’ is enabled.
‘Aspose.Words.Cloud.Examples.CSharp.exe’ (CLR v4.0.30319: DefaultDomain): Loaded ‘C:\Users\Softwareselskabet\Desktop\Aspose.Words-for-Cloud-master\Examples\DotNET\CSharp\bin\Debug\Aspose.Words.Cloud.Examples.CSharp.exe’. Symbols loaded.
‘Aspose.Words.Cloud.Examples.CSharp.exe’ (CLR v4.0.30319: Aspose.Words.Cloud.Examples.CSharp.exe): Loaded ‘C:\Users\Softwareselskabet\Desktop\Aspose.Words-for-Cloud-master\Examples\DotNET\CSharp\bin\Debug\Aspose.Words-Cloud.dll’. Skipped loading symbols. Module is optimized and the debugger option ‘Just My Code’ is enabled.
‘Aspose.Words.Cloud.Examples.CSharp.exe’ (CLR v4.0.30319: Aspose.Words.Cloud.Examples.CSharp.exe): Loaded ‘C:\Users\Softwareselskabet\Desktop\Aspose.Words-for-Cloud-master\Examples\DotNET\CSharp\bin\Debug\Aspose.Storage-Cloud.dll’. Skipped loading symbols. Module is optimized and the debugger option ‘Just My Code’ is enabled.
‘Aspose.Words.Cloud.Examples.CSharp.exe’ (CLR v4.0.30319: Aspose.Words.Cloud.Examples.CSharp.exe): Loaded ‘C:\Windows\MicrosoftNet\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll’. Skipped loading symbols. Module is optimized and the debugger option ‘Just My Code’ is enabled.
‘Aspose.Words.Cloud.Examples.CSharp.exe’ (CLR v4.0.30319: Aspose.Words.Cloud.Examples.CSharp.exe): Loaded ‘C:\Windows\MicrosoftNet\assembly\GAC_32\System.Web\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Web.dll’. Skipped loading symbols. Module is optimized and the debugger option ‘Just My Code’ is enabled.
‘Aspose.Words.Cloud.Examples.CSharp.exe’ (CLR v4.0.30319: Aspose.Words.Cloud.Examples.CSharp.exe): Loaded ‘C:\Windows\MicrosoftNet\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll’. Skipped loading symbols. Module is optimized and the debugger option ‘Just My Code’ is enabled.
‘Aspose.Words.Cloud.Examples.CSharp.exe’ (CLR v4.0.30319: Aspose.Words.Cloud.Examples.CSharp.exe): Loaded ‘C:\Windows\MicrosoftNet\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll’. Skipped loading symbols. Module is optimized and the debugger option ‘Just My Code’ is enabled.
‘Aspose.Words.Cloud.Examples.CSharp.exe’ (CLR v4.0.30319: Aspose.Words.Cloud.Examples.CSharp.exe): Loaded ‘C:\Windows\MicrosoftNet\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll’. Skipped loading symbols. Module is optimized and the debugger option ‘Just My Code’ is enabled.
Exception thrown: ‘Com.Aspose.Storage.ApiException’ in Aspose.Storage-Cloud.dll
error:The remote server returned an error: (401) Unauthorized.
{“Message”:“Authentication failed!”}
at Com.Aspose.Storage.Api.StorageApi.PutCreate(String Path, String versionId, String storage, Byte[] file)
at Mail_Merge.ExecuteTemplate.Run() in C:\Users\Softwareselskabet\Desktop\Aspose.Words-for-Cloud-master\Examples\DotNET\CSharp\Mail-Merge\ExecuteTemplate.cs:line 28
Exception thrown: ‘Com.Aspose.Storage.ApiException’ in Aspose.Storage-Cloud.dll
error:The remote server returned an error: (401) Unauthorized.
{“Message”:“Authentication failed!”}
at Com.Aspose.Storage.Api.StorageApi.PutCreate(String Path, String versionId, String storage, Byte[] file)
at Document.ConvertToAnyFormat.Run() in C:\Users\Softwareselskabet\Desktop\Aspose.Words-for-Cloud-master\Examples\DotNET\CSharp\Document\ConvertToAnyFormat.cs:line 32
The program ‘[5700] Aspose.Words.Cloud.Examples.CSharp.exe’ has exited with code 0 (0x0).

VS executeTemplate.png (126.7 KB)
RunExamples.png (120.2 KB)
Aspose.png (226.3 KB)
Common.png (155.0 KB)

@Sophus

Thank you for contacting Aspose Support.

You are using deprecated* SDK. We have released fully reworked version of .NET SDK, please check this blog post for more details.

Considering this, please use this updated SDK GitHub - aspose-words-cloud/aspose-words-cloud-dotnet: .NET library for communicating with the Aspose.Words Cloud API and let us know if you face any error.

Thanks for the quick reply. Will try it and let you know. I also posted this in the Aspose forum but cant find it and delete.

@Sophus

Please post all queries regarding Aspose REST APIs in this forum. https://forum.aspose.com/ is for native APIs.

@sohail.aspose

I created a ASPNET web app project. I followed the instructions and added Aspose.Words Cloud:
Open the Solution Explorer.
Right-click on a project within your solution.
Click on Manage NuGet Packages…
Click on the Browse tab and search for “Aspose.Words-Cloud”.
Click on the Aspose.Words-Cloud package, select the appropriate version in the right-tab and click Install.

In the old dotnet examples it was strait forward to run different examples but how do I do it with the SDK.

I want to test the mail merge functions which can be found here;

Can I somehow added it to default.aspx.cs and run ExecuteMailMergeTest.cs for example with the sample template and template data?

@Sophus

The above steps you performed are to add “Aspose.Words-Cloud” in your own project.

If you want to run SDKs Unit Tests then simply clone the repository:

git clone https://github.com/aspose-words-cloud/aspose-words-cloud-dotnet.git

Open the project (Aspose.Words.Cloud.Sdk.sln) in Visual Studio, set your App Key and App SID in OAuthTests.cs file and most importantly set AppURL to https://api.aspose.cloud/v1.1

Open the required Test file e.g. ExecuteMailMergeTest.cs and run a Unit Test e.g. TestPutExecuteMailMergeOnline() by clicking on its method name or inside method body and press
Ctrl + R then immediately Ctrl + T.

You may Google how to run Unit Tests in Visual Studio for more details.

Hi @mateensajjad.

It worked. I know this not the scope for the support forum in general. So thank you.

BR.

@Sophus

Good to know that it worked. Please keep using Aspose for Cloud APIs.

This is not working in my case. I am using the current git aspose.words.cloud sdk GitHub - aspose-words-cloud/aspose-words-cloud-dotnet: .NET library for communicating with the Aspose.Words Cloud API

|Error|CS0176|Member ‘OAuthTests.AppKey’ cannot be accessed with an instance reference; qualify it with a type name instead|Aspose.Words.Cloud.Sdk.Tests|C:\Users\welcome\Desktop\aspose-words-cloud-dotnet-master\aspose-words-cloud-dotnet-master\Aspose.Words.Cloud.Sdk.Tests\Infrastructure\OAuthTests.cs|67|Active|

@ssana

The SDK is working perfectly fine our end. Please ensure you have correctly integrated following NuGet packages in your project:

  1. Aspose.Words-Cloud (v18.9.0)
  2. Newtonsoft.Json (v18.9.0)
  3. Aspose.Storage-Cloud (v18.3.0)

Manage NuGet Packages… window should look as follows:

@Sophus, @ssana

We would like to share some updates about the Aspose.Words Cloud API. The latest API Version V4 is using JWT (JSON Web Token) for API request authentication and now it provides its own API methods for storage operations. So there is no dependency of Aspose.Storage Cloud API anymore for storage operations.

Furthermore, please note we have merged Mail Merge Template and Mustache Template APIs in API Version V4. Now single API will be used for both types of Templates.