0

我正在使用 .NET Core 3.1,并尝试通过提供私钥连接到 Google 的索引 API。我看过以下内容:使用 .NET和https://hamidmosalla.com/2019/12/07/using-google-indexing-api-with-google-api-client-library-的 Google Indexing API Batch Request 示例网/

这是我的代码:

using Google.Apis.Auth.OAuth2;
using Google.Apis.Requests;
using Google.Apis.Services;
using Google.Apis.Indexing.v3;
using Google.Apis.Indexing.v3.Data;
using Microsoft.Extensions.Hosting.Internal;

public static class MyClassDownloader {
    public static GoogleCredential GetGoogleCredential()
    {    
        string path = HostingEnvironment.MapPath("/PrivateKey/myprivatekey.json");
        GoogleCredential credential;
    
        using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
        {
            credential = GoogleCredential.FromStream(stream).CreateScoped(new[] { "https://www.googleapis.com/auth/indexing" });
        }
    
        return credential;
    }
}

但是,我收到关于以下内容的错误MapPath

'HostingEnvironment' does not contain a definition for 'MapPath'

我试过调查IHostingEnvironment,之后我得知它已被弃用IWebHostEnvironment。无论我是处于开发阶段还是生产阶段,如何为我的 JSON 文件获取正确的物理路径?

4

1 回答 1

0

我将更改MyClassDownloader为非静态并添加一个构造函数,该构造函数将IWebHostEnvironment作为参数保存在MyClassDownloader. 然后,MyClassDownloader需要在Startup.ConfigureServices(). 最后在任何地方都MyClassDownloader.GetGoogleCredential()被调用(例如在控制器中),MyClassDownloader需要一个可用的实例。这个实例是通过将它注入到使用类(例如控制器)构造函数中来实现的。

示例(我没有测试该代码):

// MyClassDownloader.cs
// Make MyClassDownloader non static and add constructor
public class MyClassDownloader 
{
    private readonly IWebHostEnvironment webHostEnvironment;

    public MyClassDownloader(IWebHostEnvironment webHostEnvironment) 
    {
        this.webHostEnvironment = webHostEnvironment;
    }

    // Method not static any more
    public GoogleCredential GetGoogleCredential()
    {    
        // Depending on the hosting OS you might have to use backslash but I'm
        // not sure about that
        string path = System.IO.Path.Combine(
            webHostEnvironment.WebRootPath, 
            "/PrivateKey/myprivatekey.json");

        GoogleCredential credential;
    
        using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
        {
            credential = GoogleCredential.FromStream(stream).CreateScoped(new[] { "https://www.googleapis.com/auth/indexing" });
        }
    
        return credential;
    }
}

// Startup.cs
public class Startup 
{
    // ... some code

    public void ConfigureServices(IServiceCollection services)
    {
        // ... some code
        
        // Register newly non static MyClassDownloader so it can be injected
        // into classes that need it.
        services.AddTransient<MyClassDownloader>();

        // ... some more code
    }

    // ... some more code
}

// GoogleCredentialConsumer.cs
// Sample class that needs an instance of GoogleCredential. 
// To obtain the GoogleCredential instance, it needs a MyClassDownloader. 
// Might be a controller or another class
public class GoogleCredentialConsumer 
{
    private readonly MyClassDownloader myClassDownloader;

    public GoogleCredentialConsumer(MyClassDownloader myClassDownloader)
    {
        this.myClassDownloader = myClassDownloader;
    }

    public void MethodThatNeedsGoogleCredential()
    {
        var theGoogleCredential = myClassDownloader.GetGoogleCredential();
        // yay, finally I have it!
    }
}
于 2020-09-01T08:22:20.177 回答