我正在使用 .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 文件获取正确的物理路径?