softwerkab / fortnox-csharp-api-sdk

.NET SDK for Fortnox API.
MIT License
52 stars 64 forks source link

Authorization: Moving from StaticTokenAuth to StandardAuth #233

Open LennyHoglund opened 2 years ago

LennyHoglund commented 2 years ago

We have upgraded to Fortnox SDK 4.3.0. After quite a lot of "plumbing" we have finally got the async version to work. However, a remaining issue is to use the StandardAuth instead of StaticTokenAuth. We have tried to use your examples but we can not make them to work. Please give use some basic instrucktions how we can achive this using c# in a Web pages project.

Thanks, Lennart Hoglund

richardrandak commented 2 years ago

Can you tell me a little more about what was the problem with using the async version?

By Web pages do you mean the Razor pages in ASP.NET Core? Can you tell me what problem are you stuck at? Is there some error you can't resolve?

LennyHoglund commented 2 years ago

Hello Richard,

Thanks for your quick reply.

The issues we encountered when moving to the async version was all in our program logic. Moving to an async model mean that you don’t have a direct control of updates related to the Fortnox database. We have after several rewritings solved that issue.

We use Visual Studio 2022, Master pages projects written in c#

We want to change from StaticTokenAuth to StandardAuth using the below example code:

//var authorization = new StaticTokenAuth(AccessToken, ClientSecret); var authorization = new StandardAuth(AccessToken); var fortnoxClient = new FortnoxClient(authorization); var Connector = fortnoxClient.CompanySettingsConnector;

            try
            {
                var company = await Connector.GetAsync().ConfigureAwait(false);

                // ----------< Synchronize Company >-------------------------------------------------------------------
                SQLServer.CompanyRow["Bankgiro"] = company.BG;
                SQLServer.CompanyRow["BranchCode"] = company.BranchCode;
                SQLServer.CompanyRow["FTaxOption"] = company.TaxEnabled;
                SQLServer.CompanyRow["Postgiro"] = company.PG;
                SQLServer.CompanyRow["RegistrationNumber"] = company.OrganizationNumber;
                SQLServer.CompanyRow["VATNumber"] = company.VATNumber;
                SQLServer.CompanyRow["WebAddress"] = company.WWW;
                SQLServer.CompanyRow["SynchronizationDate"] = DateTime.Now;

                new SQLServer().Company_Update(Session, "Update");

Our current AccessToken don’t work with StandardAuth, we get the message – not authorized. How do we get a new AccessToken? We have tried your examples but can’t get them to work in our environment. Can you supply some detailed examples written in c#?

We like the new improvements in version 4.3.0. We need a little help to get over the line and be fully adapted to version 4.3.0.

Best Regards, Lennart Hoglund

From: richardrandak @.> Sent: den 17 februari 2022 12:09 To: FortnoxAB/csharp-api-sdk @.> Cc: Lennart Hoglund @.>; Author @.> Subject: Re: [FortnoxAB/csharp-api-sdk] Authorization: Moving from StaticTokenAuth to StandardAuth (Issue #233)

Can you tell me a little more about what was the problem with using the async version?

By Web pages do you mean the Razor pages in ASP.NET Core? Can you tell me what problem are you stuck at? Is there some error you can't resolve?

— Reply to this email directly, view it on GitHubhttps://github.com/FortnoxAB/csharp-api-sdk/issues/233#issuecomment-1042835887, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ADNWVAYJKEZQVQCEPAZKYM3U3TJM5ANCNFSM5OUG6TKA. Triage notifications on the go with GitHub Mobile for iOShttps://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Androidhttps://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub. You are receiving this because you authored the thread.Message ID: @.**@.>>

richardrandak commented 2 years ago

Hi Lennart! First of all, the access token used in the old auth won't work with the new auth. Getting an access token in the new auth requires a user interaction, specifically, redirecting him to an fortnox auth uri, handling a callback in your app and managing the token life span. I assume you have read the wiki right? https://github.com/FortnoxAB/csharp-api-sdk/wiki/FortnoxAuth

One more thing.. did you consider whether need to switch to the new workflow? Are there new users using your app ?

LennyHoglund commented 2 years ago

Hi Richard,

I will study the information in your enclosed link and see if I can work out a solution suitable for our application. Our application is under development. All new customers will be new users so I believe we have to switch to the new workflow.

Thanks for your reply. I will inform you about our progress.

Best regards, Lennart Hoglund

From: richardrandak @.> Sent: den 17 februari 2022 13:46 To: FortnoxAB/csharp-api-sdk @.> Cc: Lennart Hoglund @.>; Author @.> Subject: Re: [FortnoxAB/csharp-api-sdk] Authorization: Moving from StaticTokenAuth to StandardAuth (Issue #233)

Hi Lennart! First of all, the access token used in the old auth won't work with the new auth. Getting an access token in the new auth requires a user interaction, specifically, redirecting him to an fortnox auth uri, handling a callback in your app and managing the token life span. I assume you have read the wiki right? https://github.com/FortnoxAB/csharp-api-sdk/wiki/FortnoxAuth

One more thing.. did you consider whether need to switch to the new workflow? Are there new users using your app ?

— Reply to this email directly, view it on GitHubhttps://github.com/FortnoxAB/csharp-api-sdk/issues/233#issuecomment-1042913203, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ADNWVA7OWYNEOQOJJDL46L3U3TUYHANCNFSM5OUG6TKA. Triage notifications on the go with GitHub Mobile for iOShttps://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Androidhttps://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub. You are receiving this because you authored the thread.Message ID: @.**@.>>

richardrandak commented 2 years ago

You're welcome. Unfortunetely, I don't have resources to advise you regarding the app design for the new workflow, so I hope the wiki provides enough information. It is a basically a common OAuth2 workflow, which requires redirecting a user to Fortnox auth server and handling the callback with an authorization code, which is then exchanged for access token in your app.

I will be glad to hear about your progress.

LennyHoglund commented 2 years ago

Hi Richard,

I have started to venture into OAuth2. I begin with your examples specified in “Standard (OAuth2) Auth;

var fortnoxAuthClient = new FortnoxAuthClient; var authWorkflow = fortnoxAuthClient.StandardAuthWorkflow; var authUri = authWorkflow.BuildAuthUri(ClientId, Scope.Customer, state);

Scope.Customer gives me the following error;

Argument 2: cannot convert from 'Fortnox.SDK.Auth.Scope' to 'System.Collections.Generic.IEnumerable'

Any suggestions what to do?

Cheers, Lennart

From: richardrandak @.> Sent: den 17 februari 2022 15:02 To: FortnoxAB/csharp-api-sdk @.> Cc: Lennart Hoglund @.>; Author @.> Subject: Re: [FortnoxAB/csharp-api-sdk] Authorization: Moving from StaticTokenAuth to StandardAuth (Issue #233)

You're welcome. Unfortunetely, I don't have resources to advise you regarding the app design for the new workflow, so I hope the wiki provides enough information. It is a basically a common OAuth2 workflow, which requires redirecting a user to Fortnox auth server and handling the callback with an authorization code, which is then exchanged for access token in your app.

I will be glad to hear about your progress.

— Reply to this email directly, view it on GitHubhttps://github.com/FortnoxAB/csharp-api-sdk/issues/233#issuecomment-1042980327, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ADNWVA3NL6N4IYMZ7EEXTE3U3T5ULANCNFSM5OUG6TKA. Triage notifications on the go with GitHub Mobile for iOShttps://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Androidhttps://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub. You are receiving this because you authored the thread.Message ID: @.***>

richardrandak commented 2 years ago

The function takes collection of scopes. If you need just one, just make an array out of it

var fortnoxAuthClient = new FortnoxAuthClient;
var authWorkflow = fortnoxAuthClient.StandardAuthWorkflow;
var authUri = authWorkflow.BuildAuthUri(ClientId, new [] { Scope.Customer }, state);
LennyHoglund commented 2 years ago

Hi Richard,

Thank you, it works. Now I go for the remaining implantation. Lennart

From: richardrandak @.> Sent: den 22 februari 2022 12:01 To: FortnoxAB/csharp-api-sdk @.> Cc: Lennart Hoglund @.>; Author @.> Subject: Re: [FortnoxAB/csharp-api-sdk] Authorization: Moving from StaticTokenAuth to StandardAuth (Issue #233)

The function takes collection of scopes. If you need just one, just make an array out of it

var fortnoxAuthClient = new FortnoxAuthClient;

var authWorkflow = fortnoxAuthClient.StandardAuthWorkflow;

var authUri = authWorkflow.BuildAuthUri(ClientId, new [] { Scope.Customer }, state);

- Reply to this email directly, view it on GitHubhttps://github.com/FortnoxAB/csharp-api-sdk/issues/233#issuecomment-1047676913, or unsubscribehttps://github.com/notifications/unsubscribe-auth/ADNWVAZC7GMYFICYQB4SQGTU4NUGZANCNFSM5OUG6TKA. Triage notifications on the go with GitHub Mobile for iOShttps://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Androidhttps://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub. You are receiving this because you authored the thread.Message ID: @.***>