繁体   English   中英

在 ASP.NET 核心 Web ZDB974238714CA38DE634A7CE1D08 项目中使用 Google My Business API 时出现授权错误

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

我正在尝试从我的 ASP.NET Core Web ZDB974238714CA8A14A7CE1D0 项目中调用 Google My Business API,如下所示:

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);
            }
        }
    }
}

在网站https://code.google.com/apis/console上,我已经注册了我的应用程序,为我的应用程序设置了生成的客户端 ID:和客户端密码,并尝试使用 Google 登录。 不幸的是,我收到了这个错误:

在此处输入图像描述

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.

您需要将重定向 URI 添加到您查看客户端密码的客户端 OAuth 下的 Google 云设置中,如下所述: https://developers.google.com/my-business/content/basic-setup#:~ :text=添加%20the%20following%20as%20a%20valid%20redirect%20URI%3A

但是请注意,如果您尝试实现服务器到服务器,这不是正确的路径,您需要验证服务器端而不是客户端,就像这里发生的那样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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