Google Oauth error: At least one client secrets (Installed or Web) should be set
Google Oauth error: At least one client secrets (Installed or Web) should be set
我正在使用 Google 的 Oauth 2.0 通过我们的服务器将视频上传到 Youtube。
我的客户端 ID 是 "service account"。我下载了 json 密钥并将其添加到我的解决方案中。
相关代码如下:
private async Task Run(string filePath)
{
UserCredential credential;
var keyUrl = System.Web.HttpContext.Current.Server.MapPath("~/content/oauth_key.json");
using (var stream = new FileStream(keyUrl, FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
// This OAuth 2.0 access scope allows an application to upload files to the
// authenticated user's YouTube channel, but doesn't allow other types of access.
new[] { YouTubeService.Scope.YoutubeUpload },
"user",
CancellationToken.None
);
}
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
});
当我 运行 它时,我收到此错误:至少应设置一个客户端机密(已安装或 Web)。
然而,在我的json中没有"client secret":
{
"private_key_id": "9d98c06b3e730070806dcf8227578efd0ba9989b",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIICdQIBADANBgkqhk etc,
"client_email": "546239405652-8igo05a5m8cutggehk3rk3hspjfm3t04@developer.gserviceaccount.com",
"client_id": "546239405652-8igo05a5m8cutggehk3rk3hspjfm3t04.apps.googleusercontent.com",
"type": "service_account"
}
所以我想我忽略了什么。
也许我不能使用 "service account" ?不知道...
不是 C# 专家,但您似乎在尝试使用服务帐户来执行 OAuth2 网络服务器流程,这应该行不通。
您可能想改用 ServiceAccountCredential。
有关不同 Google OAuth2 流程的更多信息,请参阅 web server, service account 等的文档
我设法获得了一个服务帐户来处理 P12 文件,但想知道如何使用 JSON 文件,或者只是 JSON 文件中的值来创建证书.
获取代币
private static String GetOAuthCredentialViaP12Key()
{
const string serviceAccountEmail = SERVICE_ACCOUNT_EMAIL;
var certificate = new X509Certificate2(SERVICE_ACCOUNT_PKCS12_FILE_PATH, "notasecret", X509KeyStorageFlags.Exportable);
var scope = DriveService.Scope.Drive + " https://spreadsheets.google.com/feeds";
var credential = new ServiceAccountCredential( new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { scope }
}.FromCertificate(certificate) );
if (credential.RequestAccessTokenAsync(CancellationToken.None).Result == false)
{
return null;
}
return credential.Token.AccessToken;
}
这就是我使用我得到的令牌的方式
// Initialize the variables needed to make the request
OAuth2Parameters parameters = new OAuth2Parameters {AccessToken = token};
GOAuth2RequestFactory requestFactory = new GOAuth2RequestFactory(null, "MySpreadsheetIntegration-v1", parameters);
SpreadsheetsService service = new SpreadsheetsService("MySpreadsheetIntegration-v1");
service.RequestFactory = requestFactory;
使用 json
文件的解决方案非常相似。
这里是创建 VisionService
使用 GoogleCredential
对象创建 json
文件 ServiceAccountCredential
.
的示例
GoogleCredential credential;
using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
credential = GoogleCredential.FromStream(stream)
.CreateScoped(VisionService.Scope.CloudPlatform);
}
var service = new VisionService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "my-app-name",
});
此示例需要两个 NuGet
包:
Google.Apis.Vision.v1
Google.Apis.Oauth2.v2
我正在使用 Google 的 Oauth 2.0 通过我们的服务器将视频上传到 Youtube。 我的客户端 ID 是 "service account"。我下载了 json 密钥并将其添加到我的解决方案中。
相关代码如下:
private async Task Run(string filePath)
{
UserCredential credential;
var keyUrl = System.Web.HttpContext.Current.Server.MapPath("~/content/oauth_key.json");
using (var stream = new FileStream(keyUrl, FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
// This OAuth 2.0 access scope allows an application to upload files to the
// authenticated user's YouTube channel, but doesn't allow other types of access.
new[] { YouTubeService.Scope.YoutubeUpload },
"user",
CancellationToken.None
);
}
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
});
当我 运行 它时,我收到此错误:至少应设置一个客户端机密(已安装或 Web)。
然而,在我的json中没有"client secret":
{
"private_key_id": "9d98c06b3e730070806dcf8227578efd0ba9989b",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIICdQIBADANBgkqhk etc,
"client_email": "546239405652-8igo05a5m8cutggehk3rk3hspjfm3t04@developer.gserviceaccount.com",
"client_id": "546239405652-8igo05a5m8cutggehk3rk3hspjfm3t04.apps.googleusercontent.com",
"type": "service_account"
}
所以我想我忽略了什么。 也许我不能使用 "service account" ?不知道...
不是 C# 专家,但您似乎在尝试使用服务帐户来执行 OAuth2 网络服务器流程,这应该行不通。 您可能想改用 ServiceAccountCredential。 有关不同 Google OAuth2 流程的更多信息,请参阅 web server, service account 等的文档
我设法获得了一个服务帐户来处理 P12 文件,但想知道如何使用 JSON 文件,或者只是 JSON 文件中的值来创建证书.
获取代币
private static String GetOAuthCredentialViaP12Key()
{
const string serviceAccountEmail = SERVICE_ACCOUNT_EMAIL;
var certificate = new X509Certificate2(SERVICE_ACCOUNT_PKCS12_FILE_PATH, "notasecret", X509KeyStorageFlags.Exportable);
var scope = DriveService.Scope.Drive + " https://spreadsheets.google.com/feeds";
var credential = new ServiceAccountCredential( new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { scope }
}.FromCertificate(certificate) );
if (credential.RequestAccessTokenAsync(CancellationToken.None).Result == false)
{
return null;
}
return credential.Token.AccessToken;
}
这就是我使用我得到的令牌的方式
// Initialize the variables needed to make the request
OAuth2Parameters parameters = new OAuth2Parameters {AccessToken = token};
GOAuth2RequestFactory requestFactory = new GOAuth2RequestFactory(null, "MySpreadsheetIntegration-v1", parameters);
SpreadsheetsService service = new SpreadsheetsService("MySpreadsheetIntegration-v1");
service.RequestFactory = requestFactory;
使用 json
文件的解决方案非常相似。
这里是创建 VisionService
使用 GoogleCredential
对象创建 json
文件 ServiceAccountCredential
.
GoogleCredential credential;
using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
credential = GoogleCredential.FromStream(stream)
.CreateScoped(VisionService.Scope.CloudPlatform);
}
var service = new VisionService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "my-app-name",
});
此示例需要两个 NuGet
包:
Google.Apis.Vision.v1
Google.Apis.Oauth2.v2