Skip to content

Allow directly specifying access_token to use in credentials #761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
salrashid123 opened this issue Jun 21, 2016 · 18 comments
Closed

Allow directly specifying access_token to use in credentials #761

salrashid123 opened this issue Jun 21, 2016 · 18 comments

Comments

@salrashid123
Copy link

Allow specifying an access_token directly into a Credential
for reference, see:
googleapis/google-cloud-node#1346

I didn't see a credential type here that would allow for this:
https://developers.google.com/api-client-library/dotnet/reference/1.10.0/classGoogle_1_1Apis_1_1Auth_1_1OAuth2_1_1GoogleCredential#details

@jskeet
Copy link
Collaborator

jskeet commented Jun 22, 2016

@salrashid123
Copy link
Author

I doubt that'll do it. Usercredentials doesn't have a way to directly plugin an access token (its designed for webflows and processing full tokenresponses w/ refresh).
What asked here is for a credential to initialize with just a raw token
the analog in python is AccessTokenCredentials

@jskeet
Copy link
Collaborator

jskeet commented Jun 22, 2016

I don't see what you mean about UserCredentials not having a way to plug in an access token - it has a constructor that takes a TokenResponse... can't you just pass in a TokenResponse which is set to never expire (but has the token prepopulated), and an IAuthorizationCodeFlow that just throws if it's ever asked to refresh anything?

Assuming that works, we could certainly add a shortcut to create it, but I'd like to know whether it does work first. Are you able to try that out?

@salrashid123
Copy link
Author

(sorry, didn't intend to close).
i tried to inject in raw token into the token response and then try to work it into UserCredentials but still no luck

@salrashid123 salrashid123 reopened this Jun 23, 2016
@jskeet
Copy link
Collaborator

jskeet commented Jun 23, 2016

Okay - I'll give it a go myself when I get a chance. Unlikely to be in the next couple of days though.

@salrashid123
Copy link
Author

got it working (i needed to fully specify the token w/ issue time and duration)

            // gcloud auth print-access-token
            var valid_token = "redacted";
            var invalid_token = "redacted";            
            var token = new Google.Apis.Auth.OAuth2.Responses.TokenResponse()
            {
                AccessToken = valid_token,
                ExpiresInSeconds = 3600,
                Issued = DateTime.Now
            };
            var fakeflow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
            {
                ClientSecrets = new ClientSecrets
                {
                    ClientId = "abc",
                    ClientSecret = "abc"
                }
            });
            UserCredential credential = new UserCredential(fakeflow, "blahblah", token);
            Console.WriteLine(credential.Token.AccessToken);
            var serviceInitializer = new BaseClientService.Initializer()
            {
                ApplicationName = "Storage Sample",
                HttpClientInitializer = credential
            };
            service = new StorageService(serviceInitializer);

@jskeet
Copy link
Collaborator

jskeet commented Jun 28, 2016

Great - although I'd have expected an ExpiresInSeconds value of something huge, so that it never tried to refresh at all. Or is the idea that you'd only use this token for a short time anyway?

@salrashid123
Copy link
Author

These tokens will expire and are issued with 3600s shelf life by default. Its opaque so we would not know how long its currently valid for unless we query during initialization like the following:

curl https://www.googleapis.com/oauth2/v2/tokeninfo?access_token=$(gcloud auth print-access-token)

(which we probably shoudn't do; its a round-trip)

I tried a couple of things:

  1. If the token is invalid (old, totally expired): we'll see 401 error (invalid credentials)
  2. if the token is within 60s of expiration (given issued, ExpiresInSeconds, IsExpired)...the code will attempt to refresh the token but will result in: "The access token has expired but we can't refresh it"

We can check the other language implementations but I think its fine with the logic and set it to (3600,Now); if it expires before that, you'll still see error#1.

Here's a reference implementation for python (its set to null):
oauth2client.client.AccessTokenCredentials
in .net, i had to specify the both Issued and Expired (if i didn't i'd end up seeing the error #2 even for valid creds)

@chrisdunelm
Copy link
Contributor

Closing, as seems to be resolved.

@salrashid123
Copy link
Author

@chrisdunelm I don't think this FR is implemented yet
(all did in #761 (comment) is hack a workaround/POC). it'd really need to get baked into the library directly.

for ref again, its there in java and heres's the one for node
googleapis/google-cloud-node#1346

@chrisdunelm
Copy link
Contributor

@salrashid123 Thanks for the clarification. I'll take a look at this next week.

@chrisdunelm
Copy link
Contributor

@salrashid123 Please can you confirm #1062 implements this feature as required?

chrisdunelm added a commit to chrisdunelm/google-api-dotnet-client that referenced this issue Jul 17, 2017
@salrashid123
Copy link
Author

look good; thanks
used the following to verify:

using Google.Cloud.Storage.V1;

//GoogleCredential credential = GoogleCredential.GetApplicationDefault();
GoogleCredential credential = GoogleCredential.FromAccessToken("__redacted___");

var client = StorageClient.Create(credential);
foreach (var obj in client.ListObjects("uspto-pair", ""))
        Console.WriteLine( "  " + obj.Name + "  ");

with a .csproj referencing your current git clone https://github.com/chrisdunelm/google-api-dotnet-client.git) directly

$ dotnet --info
.NET Command Line Tools (1.0.1)

Product Information:
 Version:            1.0.1
 Commit SHA-1 hash:  005db40cd1
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="../Google.Apis.Core/Google.Apis.Core.csproj" />
    <ProjectReference Include="../Google.Apis/Google.Apis.csproj" />
    <ProjectReference Include="../Google.Apis.Auth/Google.Apis.Auth.csproj" />
    <ProjectReference Include="../Google.Apis.Auth.PlatformServices/Google.Apis.Auth.PlatformServices.csproj" />
    <PackageReference Include="Google.Cloud.Storage.V1" Version="1.0.0" />
  </ItemGroup>

</Project>


invalid and expired token gives

ERROR: Google.Apis.Requests.RequestError
Invalid Credentials [401]
Errors [
	Message[Invalid Credentials] Location[Authorization - header] Reason[authError] Domain[global]
]


@chrisdunelm
Copy link
Contributor

@salrashid123 thanks for the testing and confirmation. I've just added an integration test in #1065.

@EmilAlipiev
Copy link

why does the GoogleAuthorizationCodeFlow requires ClientSecret ? If I have a token using Android application which doesnt have client secret, how can I provide it and why I cant use my existing Token?

var fakeflow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId = "abc",
ClientSecret = "abc"
}
});

@chrisdunelm
Copy link
Contributor

@EmilAlipiev GoogleAuthorizationCodeFlow is for when using oauth2 authentication. It requires a ClientSecret to identify the credentials to use.
If you already have an access-token, you can use GoogleCredential.FromAccessToken(...).

If this isn't what you're looking for, please can you explain in more detail what you're trying to do. Thanks.

@EmilAlipiev
Copy link

I created a SO question. please see the entire question there

@chrisdunelm
Copy link
Contributor

As already answered in the SO question, we don't support Xamarin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants