BingAds / BingAds-dotNet-SDK

Other
27 stars 42 forks source link

what Authentication object to use when you want to use existing OAuthTokens from local saved file. #88

Closed litan1106 closed 4 years ago

litan1106 commented 4 years ago

For example: I want to call the reporting service with the saved the OAuthToken from local file.

qitia commented 4 years ago

hi @litan1106, in fact you should not save the total OAuth info in local file, as the access token is expected to be expired in around 1 hour. you could save the refresh token somewhere and use it with RequestAccessAndRefreshTokensAsync, Both OAuthDesktopMobileAuthCodeGrant and OAuthWebAuthCodeGrant support this method.

You should also know that the refresh token is also expired in some time/cases, so make sure you update your saved refresh token when you get a new one. see here for more detail.

litan1106 commented 4 years ago

@qitia I was trying reuse the access token across multiple report requests in the C# Bing Ads SDK. @eric-urban showed us a way to do that in Bing Ads Java SDK. However, I couldn't find a way to do in the C# world.

qitia commented 4 years ago

I think java and dotnet share the exact same concept in this part :). Please show us which is not feasible to c# in Eric's sample. I still strongly recommend to store the refresh token instead. I cannot understand why it is not fit for your case.

@eric-urban FYI.

eric-urban commented 4 years ago

@litan1106 I did not yet try this via .NET. Regarding your question about SetAuthenticationFieldsOnApiRequestObject, have you tried setting apiRequest.AuthenticationToken = YOUR_ACCESS_TOKEN i.e., similar as via OAuthAuthorization?

litan1106 commented 4 years ago

@litan1106 I did not yet try this via .NET. Regarding your question about SetAuthenticationFieldsOnApiRequestObject, have you tried setting apiRequest.AuthenticationToken = YOUR_ACCESS_TOKEN i.e., similar as via OAuthAuthorization?

I tried it the BACustomOAuth but I ran into the exception. (The SetAuthenticationFieldsOnApiRequestObject was called by the https://github.com/BingAds/BingAds-dotNet-SDK/blob/master/BingAdsApiSDK/ServiceClient.cs#L194)

System.AggregateException: One or more errors occurred. (Submit download operation failed.)
 ---> Microsoft.BingAds.V13.Reporting.CouldNotSubmitReportingDownloadException: Submit download operation failed.
 ---> System.NotImplementedException: The method or operation is not implemented.
   at ClientDashboard.Infrastructure.BingAds.BACustomOAuth.SetAuthenticationFieldsOnApiRequestObject(Object apiRequest) in C:\Codes\ClientDashboard\src\Infrastructure\Bi
ngAds\BACustomOAuth.cs:line 19
eric-urban commented 4 years ago

@litan1106 this works for me (for testing, I added a class here):

using System;
using System.Net.Http.Headers;
using Microsoft.BingAds;

namespace BingAdsExamplesLibrary.v13
{
    public class BACustomOAuth : Authentication
    {
        private string AccessToken { get; set; }

        public BACustomOAuth(string accessToken)
        {
            AccessToken = accessToken;
        }

        protected override void SetAuthenticationFieldsOnApiRequestObject(dynamic apiRequest)
        {
            if (AccessToken == null)
            {
                throw new InvalidOperationException("OAuth access token is missing or invalid.");
            }

            apiRequest.AuthenticationToken = AccessToken;
        }

        protected override void AddAuthenticationHeaders(HttpRequestHeaders requestHeaders)
        {
            requestHeaders.Add("AuthenticationToken", AccessToken);
        }
    }
}

Does this help?

litan1106 commented 4 years ago

It worked. Thanks for the help. @eric-urban @qitia