【问题标题】:Connecting to Google Sheets through Azure Web Application通过 Azure Web 应用程序连接到 Google Sheets
【发布时间】:2020-01-22 13:58:53
【问题描述】:

我正在开发一个 .net 核心项目,试图连接到谷歌表格。 在我的本地主机上运行时,一切顺利,通过 azure 发布应用程序时,我无法连接收到此错误:

加载资源失败:服务器响应状态为 502 (Bad Gateway)

这是我的代码:

                UserCredential credential;
                using (var stream =
                    new FileStream("client_id_1.json", FileMode.Open, FileAccess.Read))
                { 
                    string credPath = "token.json";
                    credPath = Path.Combine(credPath, ".credentials/sheets.googleapis.com-dotnet-quickstart.json");
                    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                        GoogleClientSecrets.Load(stream).Secrets,
                        Scopes,
                        "user",
                        CancellationToken.None,
                        new FileDataStore(credPath, true)).Result;
                }
                Service = new SheetsService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName = ApplicationName,
                });

文件“client_id_1.json”包含以下内容:

{
  "web": {
    "client_id": "...",
    "project_id": "...",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_secret": "...",
    "redirect_uris": [ "https://{{mySiteHomePage}}" ],
    "javascript_origins": [ "https://{{mySite}}" ]
  }
}

我有什么遗漏吗?我该如何解决这个问题?

【问题讨论】:

    标签: c# azure google-oauth google-sheets-api google-api-dotnet-client


    【解决方案1】:

    GoogleWebAuthorizationBroker.AuthorizeAsync 专为已安装的应用程序而设计,它会在当前机器上打开授权 Web 浏览器,这就是它运行 localhost 的原因。如果您尝试运行该托管,它将尝试在服务器上打开授权 Web 浏览器,这将无法正常工作。

    Google .net 客户端库确实支持 asp .net 核心授权帮助。

    【讨论】:

      猜你喜欢
      • 2021-12-06
      • 2018-09-24
      • 1970-01-01
      • 1970-01-01
      • 2015-12-24
      • 2016-09-21
      • 2016-02-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多