C# 如果我已经拥有访问令牌的值,如何创建UserCredential的实例?

C# 如果我已经拥有访问令牌的值,如何创建UserCredential的实例?,c#,asp.net-mvc,google-api-dotnet-client,google-plus-domains,C#,Asp.net Mvc,Google Api Dotnet Client,Google Plus Domains,所以我有这个代码 我的问题是,如果我已经通过OAuth进行了身份验证,那么如何配置用户凭据 当前的情况是,代码将显示另一个重定向到google的登录页面。既然我已经使用asp.net MVC通过OAuth进行了身份验证,并且我已经有了令牌,那么如何摆脱GoogleWebAuthorizationBroker并直接传递令牌 string[] scopes = new string[] {PlusService.Scope.PlusLogin,

所以我有这个代码
我的问题是,如果我已经通过OAuth进行了身份验证,那么如何配置用户凭据

当前的情况是,代码将显示另一个重定向到google的登录页面。既然我已经使用asp.net MVC通过OAuth进行了身份验证,并且我已经有了令牌,那么如何摆脱GoogleWebAuthorizationBroker并直接传递令牌

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>();
string[]scopes=新字符串[]{PlusService.Scope.PlusLogin,
PlusService.Scope.UserinfoEmail,
PlusService.Scope.UserinfoProfile};
用户凭据凭据=
GoogleWebAuthorizationBroker.AuthorizationAsync(新客户端机密)
{
ClientId=“xxx.apps.googleusercontent.com”,
ClientSecret=“xxxx”
},
范围,
Environment.UserName,
取消令牌。无,
新文件数据存储(“存储”)。结果;
PlusService服务=新的PlusService(新的BaseClientService.Initializer()
{
HttpClientInitializer=凭证,
ApplicationName=“ArcaneChatV2”,
});
PeopleResource.ListRequest listPeople=service.People.List(“我”,PeopleResource.ListRequest.CollectionEnum.Visible);
listPeople.MaxResults=10;
PeopleFeed PeopleFeed=listPeople.Execute();
var people=新列表();

我对这类东西还不熟悉。

假设您已经有代币,您可以执行以下操作

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); 
然后,您可以将凭据传递给服务的初始值设定项


参考我们可以直接从访问令牌创建
GoogleCredential
,并使用它来代替
UserCredential
。我只是觉得这可能对某人有帮助。以下是
GmailService
初始化的代码片段:

GoogleCredential cred = GoogleCredential.FromAccessToken(accessToken);
GmailService service = new GmailService(new BaseClientService.Initializer {HttpClientInitializer = cred});

只是一个后续问题@Nkosi如何在UserCredential中设置刷新令牌?credential.Token.refreshttoken=“[your_access_Token_here]”这是正确的方法吗?因为我一直收到“访问令牌已过期,但我们无法刷新它”。@PrinceJea。创建
TokenResponse
时包括刷新令牌。GoogleAuthorizationCodeFlow将根据您的刷新令牌和客户端机密获取新的访问令牌。如何准确获取刷新令牌?这是我尝试过的context.Identity.AddClaim(新声明(“urn:google:refreshtToken”,context.refreshtToken,ClaimValueTypes.String,“google”);我还是会出错。你看过我链接的参考资料了吗?我想我在你的另一个问题中引用了一些东西,如果你没有刷新令牌,但是你得到一个错误,表明你的令牌已经过期,那么你需要在令牌响应上设置ExpiresInSeconds属性,如果你像我一样进行一次性服务器调用,那么似乎可以防止需要刷新令牌。我发现60秒不起作用,但更大的数字起作用。我知道这不是很科学。这很有效,但是你知道我如何刷新这个令牌吗?它非常有用!正是我需要的,谢谢!