简体   繁体   中英

Getting Authorization error while using Google My Business API in ASP.NET Core Web API project

I am trying to call Google My Business API from my ASP.NET Core Web API project as shown here below:

using HighrangeModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using Google.Apis.Auth.OAuth2;
using Google.Apis.MyBusinessAccountManagement.v1;
using Google.Apis.Services;
using System.Net;
using Google.Apis.Auth.OAuth2.Responses;

namespace HighrangeAppliances.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class GMBController : ControllerBase
    {
        [Route("~/api/GetGMBAccount")]
        [HttpGet]
        public  HttpResponseMessage GetGMBAccount()
        {
            var ClientId = "XXXXXXXXXXXXXXXX.apps.googleusercontent.com";
            var ClientSecret = "XXXXXXXXXXXXXXXXXXX";
            string[] scopes = new string[] { "https://www.googleapis.com/auth/business.manage" };

            try
            {
                UserCredential credential =  GoogleWebAuthorizationBroker.AuthorizeAsync(
                new ClientSecrets {
                    ClientId = ClientId,
                    ClientSecret = ClientSecret,
                },
                scopes,
                "user",
                CancellationToken.None).Result;

                // TokenResponse contains the tokens, access token expiry time etc.
                TokenResponse token = credential.Token;

                var service = new MyBusinessAccountManagementService(new BaseClientService.Initializer() { HttpClientInitializer = credential });

                var accountsListResponse = service.Accounts.List().Execute();

                return new Response<string>("Listed Succesfully", JsonConvert.SerializeObject(accountsListResponse), HttpStatusCode.OK);
            }
            catch (Exception ex)
            {
                return new Response<string>(ex.Message, "", HttpStatusCode.ServiceUnavailable);
            }
        }
    }
}

On the website https://code.google.com/apis/console I have registered my application, set up generated Client ID: and Client Secret to my app and tried to log in with Google. Unfortunately, I got this error:

在此处输入图像描述

Now I am using localhost to check the code and under Authorized JavaScript origins: https://localhost:44386 , Authorized redirect URIs: https://localhost:44386/api/GetGMBAccount are registered.

You will need to add the redirect URI to your google cloud settings under the Client OAuth where you view your client secret, as mention here: https://developers.google.com/my-business/content/basic-setup#:~:text=Add%20the%20following%20as%20a%20valid%20redirect%20URI%3A

BUT NOTE if you are trying to implement server to server this is not the right path to follow you need to authenticate server side not client side as what is happening here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM