.net Google Api需要用户ID才能访问\u令牌
我收到了我的.net Google Api需要用户ID才能访问\u令牌,.net,google-api,google-oauth,google-api-dotnet-client,.net,Google Api,Google Oauth,Google Api Dotnet Client,我收到了我的授权码,希望将其交换到服务器上的授权令牌。NET Google API在flow类中有ExchangeCodeForTokenAsync方法。似乎是我需要的,但它需要userId作为其参数之一 var flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer { ClientSecrets = new ClientSecrets
授权码
,希望将其交换到服务器上的授权令牌
。NET Google API在flow类中有ExchangeCodeForTokenAsync
方法。似乎是我需要的,但它需要userId作为其参数之一
var flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId = "...",
ClientSecret = "..."
},
Scopes = new[] { "some scopes" },
});
flow.ExchangeCodeForTokenAsync(userId, code, "redirect url", CancellationToken.None); //exchange token requires userId
那么,我应该在收到授权令牌之前注册用户吗?但在我得到他的谷歌个人资料之前我不能这么做(我需要一个代币)
不幸的是,没有.NET客户端的文档,但是(在网页上搜索“web服务器收到授权代码后”以访问相关部分)没有提到用户ID
因此,要么我试图使用错误的方法,要么.NET Api错误,要么我错过了一些明显的东西。要通过access\u令牌交换授权代码,您需要
也许你说expect
userId
是client\u id
…我不知道库…如果说在这个ExchangeCodeForTokenAsync
方法中expect是userId而不是client\u id,那么可能是一个bug。要通过访问令牌交换授权代码,你需要
也许你说expect
userId
是client\u id
…我不知道库…如果说在这个ExchangeCodeForTokenAsync
方法中expect是userId而不是client\u id,那么这可能是一个错误。让我们从获取用户的配置文件信息开始
有两个API可用于此People API和google+API
以下方法将请求用户访问。每个用户都可以通过发送不同的用户名进行更改。凭据信息将存储在%appData%中。您可以阅读有关此信息的详细信息
//
///此方法使用Oauth2向用户请求授权。
///凭据存储在System.Environment.SpecialFolder.Personal中
///文件https://developers.google.com/accounts/docs/OAuth2
///
///从Google开发者控制台到客户端机密json文件的路径。
///正在授权的用户的标识字符串。
///DriveService用于针对驱动器API发出请求
公共静态PeopleService AuthenticateAuth(字符串clientSecretJson,字符串用户名)
{
尝试
{
if(string.IsNullOrEmpty(用户名))
抛出新的ArgumentNullException(“用户名”);
if(string.IsNullOrEmpty(clientSecretJson))
抛出新ArgumentNullException(“clientSecretJson”);
如果(!File.Exists(clientSecretJson))
抛出新异常(“clientSecretJson文件不存在。”);
//这些是您需要的权限范围。最好只请求您需要的权限,而不是所有权限
string[]scopes=新字符串[]{PeopleService.Scope.UserPhoneNumberRead,//查看您的电话号码
PeopleService.Scope.UserAddressesRead,//查看您的街道地址
PeopleService.Scope.UserBirthdayRead,//查看您的完整出生日期
PeopleService.Scope.ContactsReadonly,//查看您的联系人
PeopleService.Scope.UserEmailsRead,//查看您的电子邮件地址
PeopleService.Scope.UserinfoProfile,//查看您的基本配置文件信息
PeopleService.Scope.UserinfoEmail,//查看您的电子邮件地址
PeopleService.Scope.PlusLogin,//了解您的基本个人资料信息和您圈子中的人员列表。
PeopleService.Scope.Contacts};//管理您的联系人
用户凭证;
使用(var stream=newfilestream(clientSecretJson,FileMode.Open,FileAccess.Read))
{
字符串credPath=System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
credPath=Path.Combine(credPath,“.credentials/”,System.Reflection.Assembly.getExecutionGassembly().GetName().Name);
//请求身份验证或加载以前存储的用户名身份验证
credential=GoogleWebAuthorizationBroker.AuthorizationAsync(GoogleClientSecrets.Load(stream.Secrets),
范围,
用户名,
取消令牌。无,
新文件数据存储(credPath,true))。结果;
}
//创建驱动器API服务。
返回新的PeopleService(新的BaseClientService.Initializer()
{
HttpClientInitializer=凭证,
应用程序a
/// <summary>
/// This method requests Authentcation from a user using Oauth2.
/// Credentials are stored in System.Environment.SpecialFolder.Personal
/// Documentation https://developers.google.com/accounts/docs/OAuth2
/// </summary>
/// <param name="clientSecretJson">Path to the client secret json file from Google Developers console.</param>
/// <param name="userName">Identifying string for the user who is being authentcated.</param>
/// <returns>DriveService used to make requests against the Drive API</returns>
public static PeopleService AuthenticateOauth(string clientSecretJson, string userName)
{
try
{
if (string.IsNullOrEmpty(userName))
throw new ArgumentNullException("userName");
if (string.IsNullOrEmpty(clientSecretJson))
throw new ArgumentNullException("clientSecretJson");
if (!File.Exists(clientSecretJson))
throw new Exception("clientSecretJson file does not exist.");
// These are the scopes of permissions you need. It is best to request only what you need and not all of them
string[] scopes = new string[] { PeopleService.Scope.UserPhonenumbersRead, //View your phone numbers
PeopleService.Scope.UserAddressesRead, //View your street addresses
PeopleService.Scope.UserBirthdayRead, //View your complete date of birth
PeopleService.Scope.ContactsReadonly, //View your contacts
PeopleService.Scope.UserEmailsRead, //View your email addresses
PeopleService.Scope.UserinfoProfile, //View your basic profile info
PeopleService.Scope.UserinfoEmail, //View your email address
PeopleService.Scope.PlusLogin, //Know your basic profile info and list of people in your circles.
PeopleService.Scope.Contacts}; //Manage your contacts
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/", System.Reflection.Assembly.GetExecutingAssembly().GetName().Name);
// Requesting Authentication or loading previously stored authentication for userName
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets,
scopes,
userName,
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
}
// Create Drive API service.
return new PeopleService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "People Oauth2 Authentication Sample"
});
}
catch (Exception ex)
{
Console.WriteLine("Create Oauth2 account PeopleService failed" + ex.Message);
throw new Exception("CreateServiceAccountPeopleFailed", ex);
}
}