如何在 OAuth2Parameters 中设置多个范围?
How to set multiple scope in OAuth2Parameters?
我想在 OAuth2Parameters 中设置多个范围。如何设置多个范围?
这是我的代码。
//Scope for Calendar and Contact Group
string[] scopes = new string[] {Google.Apis.Calendar.v3.CalendarService.Scope.Calendar, Google.Apis.Calendar.v3.CalendarService.Scope.CalendarReadonly, "https://www.google.com/m8/feeds/ https://apps-apis.google.com/a/feeds/groups/"};
OAuth2Parameters parameters = new OAuth2Parameters();
parameters.ClientId = CLIENT_ID;
parameters.ClientSecret = CLIENT_SECRET;
parameters.RedirectUri = REDIRECT_URI;
parameters.Scope=scopes[0]; //here parameters.Scope has String type. I can only set only 1 scope.
如果您需要发送多个带参数的范围,则将多个 Scope
添加到一个字符串中,并以 space
作为分隔符。
var ScopeString = @"myscope/as/1 myscope/as/2" ;
然后设置这个字符串为parameter.Scope
属性.
parameters.Scope= ScopeString;
这适用于任何通用 OAuth
进程,但请验证它。
希望这有帮助
Scopes 只是一个字符串数组。只需添加更多用逗号分隔它们即可。
/// <summary>
/// The Google APIs Client Library for .net uses the client_secrets.json file format for storing the client_id, client_secret, and other OAuth 2.0 parameters.
/// The client_secrets.json file format is a JSON formatted file containing the client ID, client secret, and other OAuth 2.0 parameters.
///
/// This file can be obtained from Google Developers console: https://console.developers.google.com/project?authuser=0
/// </summary>
/// <param name="clientSecretJson">Path to the client secret json file from Google Developers console.</param>
/// <param name="userName">The user to authorize.</param>
/// <returns>a valid CalendarService</returns>
public static CalendarService AuthenticateOauth(string clientSecretJson, string userName)
{
if (string.IsNullOrEmpty(userName))
throw new Exception("userName is required for datastore.");
if (!File.Exists(clientSecretJson))
throw new Exception("Cant find Client Secret Json file.");
string[] scopes = new string[] { CalendarService.Scope.Calendar, // Manage your calendars
CalendarService.Scope.CalendarReadonly, // View your calendars
"https://www.google.com/m8/feeds/",
"https://apps-apis.google.com/a/feeds/groups/"};
try
{
UserCredential credential;
using (var stream = new FileStream(clientSecretJson, FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/Calendar");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
scopes,
userName,
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
// Create Calendar API service.
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Calendar Authentication Sample",
});
return service;
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
throw ex;
}
}
TOP 提示:“https://www.google.com/m8/feeds/ https://apps-apis.google.com/a/feeds/groups/”都是 gdata apis 不适用于当前版本的 .net 客户端库仅支持发现 APIs。话虽如此,您没有理由不能使用它来对联系人进行身份验证 API 您只是无法使用此客户端库访问联系人 api 您将不得不使用 gdata 客户端库。
我想在 OAuth2Parameters 中设置多个范围。如何设置多个范围? 这是我的代码。
//Scope for Calendar and Contact Group
string[] scopes = new string[] {Google.Apis.Calendar.v3.CalendarService.Scope.Calendar, Google.Apis.Calendar.v3.CalendarService.Scope.CalendarReadonly, "https://www.google.com/m8/feeds/ https://apps-apis.google.com/a/feeds/groups/"};
OAuth2Parameters parameters = new OAuth2Parameters();
parameters.ClientId = CLIENT_ID;
parameters.ClientSecret = CLIENT_SECRET;
parameters.RedirectUri = REDIRECT_URI;
parameters.Scope=scopes[0]; //here parameters.Scope has String type. I can only set only 1 scope.
如果您需要发送多个带参数的范围,则将多个 Scope
添加到一个字符串中,并以 space
作为分隔符。
var ScopeString = @"myscope/as/1 myscope/as/2" ;
然后设置这个字符串为parameter.Scope
属性.
parameters.Scope= ScopeString;
这适用于任何通用 OAuth
进程,但请验证它。
希望这有帮助
Scopes 只是一个字符串数组。只需添加更多用逗号分隔它们即可。
/// <summary>
/// The Google APIs Client Library for .net uses the client_secrets.json file format for storing the client_id, client_secret, and other OAuth 2.0 parameters.
/// The client_secrets.json file format is a JSON formatted file containing the client ID, client secret, and other OAuth 2.0 parameters.
///
/// This file can be obtained from Google Developers console: https://console.developers.google.com/project?authuser=0
/// </summary>
/// <param name="clientSecretJson">Path to the client secret json file from Google Developers console.</param>
/// <param name="userName">The user to authorize.</param>
/// <returns>a valid CalendarService</returns>
public static CalendarService AuthenticateOauth(string clientSecretJson, string userName)
{
if (string.IsNullOrEmpty(userName))
throw new Exception("userName is required for datastore.");
if (!File.Exists(clientSecretJson))
throw new Exception("Cant find Client Secret Json file.");
string[] scopes = new string[] { CalendarService.Scope.Calendar, // Manage your calendars
CalendarService.Scope.CalendarReadonly, // View your calendars
"https://www.google.com/m8/feeds/",
"https://apps-apis.google.com/a/feeds/groups/"};
try
{
UserCredential credential;
using (var stream = new FileStream(clientSecretJson, FileMode.Open, FileAccess.Read))
{
string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
credPath = Path.Combine(credPath, ".credentials/Calendar");
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
scopes,
userName,
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
// Create Calendar API service.
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Calendar Authentication Sample",
});
return service;
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
throw ex;
}
}
TOP 提示:“https://www.google.com/m8/feeds/ https://apps-apis.google.com/a/feeds/groups/”都是 gdata apis 不适用于当前版本的 .net 客户端库仅支持发现 APIs。话虽如此,您没有理由不能使用它来对联系人进行身份验证 API 您只是无法使用此客户端库访问联系人 api 您将不得不使用 gdata 客户端库。