gpt4 book ai didi

c# - 如果我已经拥有 Access Token 的值,如何创建 UserCredential 的实例?

转载 作者:可可西里 更新时间:2023-11-01 09:06:09 26 4
gpt4 key购买 nike

所以我有这段代码
我的问题是,如果我已经通过 OAuth 进行身份验证,我该如何配置 UserCredential

目前的情况是代码将显示另一个登录页面重定向到谷歌。由于我已经使用 asp.net MVC 通过 OAuth 进行了身份验证并且我已经拥有 token 如何摆脱 GoogleWebAuthorizationBroker 并直接传递 token ?

string[] scopes = new string[] {PlusService.Scope.PlusLogin,
PlusService.Scope.UserinfoEmail,
PlusService.Scope.UserinfoProfile};

UserCredential credential =
GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets
{
ClientId = "xxx.apps.googleusercontent.com",
ClientSecret = "xxxx"
},
scopes,
Environment.UserName,
CancellationToken.None,
new FileDataStore("Store")).Result;

PlusService service = new PlusService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "ArcaneChatV2",
});

PeopleResource.ListRequest listPeople = service.People.List("me", PeopleResource.ListRequest.CollectionEnum.Visible);
listPeople.MaxResults = 10;
PeopleFeed peopleFeed = listPeople.Execute();
var people = new List<Person>();

我刚接触这种东西。

最佳答案

假设您已经拥有 token ,您可以执行以下操作

string[] scopes = new string[] {
PlusService.Scope.PlusLogin,
PlusService.Scope.UserinfoEmail,
PlusService.Scope.UserinfoProfile
};

var flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId = "xxx.apps.googleusercontent.com",
ClientSecret = "xxxx"
},
Scopes = scopes,
DataStore = new FileDataStore("Store")
});

var token = new TokenResponse {
AccessToken = "[your_access_token_here]",
RefreshToken = "[your_refresh_token_here]"
};

var credential = new UserCredential(flow, Environment.UserName, token);

然后您可以将您的凭据传递给服务的初始化程序

引用 Google API Client Libraries > .NET > OAuth 2.0

关于c# - 如果我已经拥有 Access Token 的值,如何创建 UserCredential 的实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38390197/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com