Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a instance of UserCredential if I already have the value of Access Token?

So I have this code
My question is how do I configure the UserCredential if I'm already authenticated via OAuth?

The current scenario is the code will display another login page redirecting to google. Since I'm already authenticated via OAuth using asp.net MVC and I already have the token how to I get rid of the GoogleWebAuthorizationBroker and directly pass the 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>();

I'm just new to this kind of stuff.

like image 806
Prince Jea Avatar asked Jul 15 '16 07:07

Prince Jea


People also ask

Where are Google access tokens stored?

There is no reason to store the access token in your database. What you should be storing is your refresh token. The refresh token can be used by your application to request a new access token as it needs.

How can I get access token authorization code?

To get a new access token, use the refresh token as you would an authorization code, but with a grant_type value of refresh_token and a refresh_token parameter that holds the contents of the refresh token. The type of grant being used. To exchange a refresh token for an access token, use refresh_token .


2 Answers

Assuming you already have the tokens you can do the following

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); 

Then you can pass your credentials to the service's initializer

Reference Google API Client Libraries > .NET > OAuth 2.0

like image 64
Nkosi Avatar answered Oct 17 '22 07:10

Nkosi


We can create GoogleCredential directly from the access token and use it instead of UserCredential as well. I just think it might be helpful for someone. Here is a code snippet of GmailService initialization:

GoogleCredential cred = GoogleCredential.FromAccessToken(accessToken);
GmailService service = new GmailService(new BaseClientService.Initializer {HttpClientInitializer = cred});
like image 25
OL. Avatar answered Oct 17 '22 07:10

OL.