CloudSQL SQLAdmin API - 意外的身份验证异常
CloudSQL SQLAdmin API - Unexpected Authentication Exception
我刚开始使用云 SQL 管理 API,设置 API 密钥,并按照 https://developers.google.com/api-client-library/dotnet/get_started#finding-information-about-the-apis.[=16 中的文档进行操作=]
相关代码:
public GoogleDatabaseApi(IRedisClientsManager redisManager, IDb connection)
{
this.redisManager = redisManager;
this.connection = connection;
this.service = new SQLAdminService(new BaseClientService.Initializer
{
ApplicationName = "cli",
ApiKey = "AIz ...WRXJU",
GZipEnabled = true
});
}
internal async Task SyncDbServersAndDatabasesAsync()
{
var res = await service.Instances.List(ProjectName).ExecuteAsync();
我在上面的最后一行收到以下异常..
Google.GoogleApiException: 'Google.Apis.Requests.RequestError
Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project. [401]
Errors [
Message[Login Required.] Location[Authorization - header] Reason[required] Domain[global]
]
我已经在 Cloud Console 中为同一个项目设置了 API 键,并且它目前不受限制(黄色警告三角形。)
我可以看到使用量在增加。
我相信 API Key 是适合我的用例的机制。当我传递合法的 API 密钥时,我不明白为什么我会看到引用 OAuth2 的异常。
- Simple API access (API keys)
These API calls do not access any private user data. Your application must authenticate itself as an application belonging to your Google API Console project. This is needed to measure project usage for accounting purposes.
API key: To authenticate your application, use an API key for your API Console project. Every simple access call your application makes must include this key.
原来我没有完整的文档。
API密钥是一种授权形式,而不是身份验证。
因此需要 OAuth V2 或 服务帐户 凭据集。
解决问题的方法是:
var credential = GoogleCredential.FromFile("credentials.json");
this.service = new SQLAdminService(new BaseClientService.Initializer
{
ApplicationName = "cli",
ApiKey = "AIz....JU",
HttpClientInitializer = credential,
GZipEnabled = true,
});
根据使用的服务帐户,API 密钥不是必需的。
我刚开始使用云 SQL 管理 API,设置 API 密钥,并按照 https://developers.google.com/api-client-library/dotnet/get_started#finding-information-about-the-apis.[=16 中的文档进行操作=]
相关代码:
public GoogleDatabaseApi(IRedisClientsManager redisManager, IDb connection)
{
this.redisManager = redisManager;
this.connection = connection;
this.service = new SQLAdminService(new BaseClientService.Initializer
{
ApplicationName = "cli",
ApiKey = "AIz ...WRXJU",
GZipEnabled = true
});
}
internal async Task SyncDbServersAndDatabasesAsync()
{
var res = await service.Instances.List(ProjectName).ExecuteAsync();
我在上面的最后一行收到以下异常..
Google.GoogleApiException: 'Google.Apis.Requests.RequestError
Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project. [401]
Errors [
Message[Login Required.] Location[Authorization - header] Reason[required] Domain[global]
]
我已经在 Cloud Console 中为同一个项目设置了 API 键,并且它目前不受限制(黄色警告三角形。)
我可以看到使用量在增加。
我相信 API Key 是适合我的用例的机制。当我传递合法的 API 密钥时,我不明白为什么我会看到引用 OAuth2 的异常。
- Simple API access (API keys)
These API calls do not access any private user data. Your application must authenticate itself as an application belonging to your Google API Console project. This is needed to measure project usage for accounting purposes.
API key: To authenticate your application, use an API key for your API Console project. Every simple access call your application makes must include this key.
原来我没有完整的文档。
API密钥是一种授权形式,而不是身份验证。
因此需要 OAuth V2 或 服务帐户 凭据集。
解决问题的方法是:
var credential = GoogleCredential.FromFile("credentials.json");
this.service = new SQLAdminService(new BaseClientService.Initializer
{
ApplicationName = "cli",
ApiKey = "AIz....JU",
HttpClientInitializer = credential,
GZipEnabled = true,
});
根据使用的服务帐户,API 密钥不是必需的。