GoogleWebAuthorizationBroker.AuthorizeAsync works locally but hangs on IIS production

I have a reporting application that uses adsense api. I am using GoogleWebAuthorizationBroker.AuthorizeAsync for authentication. When I find it locally, it works fine, the permission request window opens and after granting access everything works My problem is when I deploy it on the production server and run on IIS that GoogleWebAuthorizationBroker.AuthorizeAsync hangs forever. I assume that he is trying to open an authorization window on the server and cannot do this. I did not write this implementation, and it has been working for a long time, and it works great. I'm not sure what happened, and if something has changed, but now it will not work. I went in for surfing and tried different aploases, but nobody worked.When I tried to use GoogleAuthorizationCodeFlow with the value "offline" set in AccessType, but it still did not work with the provided URI. I also tried using the service account, but later found out that they are not supported for adsense. Below is a sample code

    var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                new ClientSecrets
                {
                    ClientId = ConfigurationManager.AppSettings["AdSenseClientId"],
                    ClientSecret = ConfigurationManager.AppSettings["AdSenseClientSecret"]
                },
                new string[] { AdSenseService.Scope.Adsense },
                "xxxxxxxx@gmail.com",
                CancellationToken.None).Result;
            // Create the service.
            adSenseService = new AdSenseService(new BaseClientService.Initializer
            {
                HttpClientInitializer = credential,
                ApplicationName = "My API Project"
            });
    var adClientRequest = adSenseService.Adclients.List();
        var adClientResponse = adClientRequest.Execute();

, . (API ASP.NET MVC5 Google API GoogleWebAuthorizationBroker.AuthorizeAsync , IIS), , " .

.

+4
1

, , IIS Visual Studio Dev Server. , , , , , .

:

using Google.Apis.Auth.OAuth2.Flows;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Util.Store;
using Google.Apis.YouTube.v3;
using Google.Apis.Auth.OAuth2.Web;
using Google.Apis.Services;
using Google.Apis.YouTube.v3.Data;
using System.Threading;

private void GetData()
{
    IAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(
        new GoogleAuthorizationCodeFlow.Initializer
        {
            ClientSecrets = new ClientSecrets { ClientId = "[ClientID]", ClientSecret = "[ClientSecret]" },
            DataStore = new FileDataStore("C:\\Temp", true),
            Scopes = new[] { YouTubeService.Scope.YoutubeReadonly }
        });
    var userId = "[ACCOUNTNAME]";
    var uri = Request.Url.ToString();
    var code = Request["code"];
    if (code != null)
    {
        var token = flow.ExchangeCodeForTokenAsync(userId, code, uri.Substring(0, uri.IndexOf("?")), CancellationToken.None).Result;

        // Extract the right state.
        var oauthState = AuthWebUtility.ExtracRedirectFromState(flow.DataStore, userId, Request["state"]).Result;
        Response.Redirect(oauthState);
    }
    else
    {
        var result = new AuthorizationCodeWebApp(flow, uri, uri).AuthorizeAsync(userId, CancellationToken.None).Result;

        if (result.RedirectUri != null)
        {
            // Redirect the user to the authorization server.
            Response.Redirect(result.RedirectUri);
        }
        else
        {
            // The data store contains the user credential, so the user has been already authenticated.
            var youtubeService = new YouTubeService(new BaseClientService.Initializer
            {
                ApplicationName = "MyVideoApp",
                HttpClientInitializer = result.Credential
            });

            if (youtubeService != null)
            {
                var channelsListRequest = youtubeService.Channels.List("contentDetails");
                channelsListRequest.Mine = true;
                ChannelListResponse channelsListResponse = channelsListRequest.ExecuteAsync().Result;
                foreach (var channel in channelsListResponse.Items)
                {
                    var playlistItemsListRequest = youtubeService.PlaylistItems.List("snippet, status");
                    playlistItemsListRequest.PlaylistId = channel.ContentDetails.RelatedPlaylists.Uploads;
                    var playlistItemsListResponse = playlistItemsListRequest.ExecuteAsync().Result;
                }
            }
        }
    }
}

Google, . URL- http://Console.developers.google.com.

Url API Auth > Credentials. " " URI , :

http://localhost:1234/Default.aspx http://localhost:1234/Options.aspx

+1

All Articles