googleapis_auth icon indicating copy to clipboard operation
googleapis_auth copied to clipboard

clientViaToken support?

Open tristancaron opened this issue 10 years ago • 6 comments

It could be possible to get an http_client via token?

Example, I use Chrome Extension Identity API

chrome.identity.getAuthToken().then((token) {
    clientViaToken(token).then((http_client) {
      var drive = new DriveApi(http_client);
      // ...
    });
});

tristancaron avatar Dec 03 '14 15:12 tristancaron

You should be able to create this with the existing library, somehow like this (untested):

AuthClient clientViaToken(String token, {Client baseClient}) {
  if (baseClient == null) {
    baseClient = new BrowserClient();
  } else {
    baseClient = nonClosingClient(baseClient);
  }

  var expiry = (new DateTime.now()).add(new Duration(hours: 1));
  var accessToken = new AccessToken('Bearer', token, expiry);
  var credentials = new AccessCredentials(accessToken, null, []);

  return authenticatedClient(baseClient, credentials);
}

Scarygami avatar Dec 03 '14 15:12 Scarygami

So currently the following snippet would be necessary:

chrome.identity.getAuthToken().then((token) {
  var client = new http.Client();
  var expiryDate = ???;
  var accessToken = new AccessToken('Bearer', token, expiryDate);
  var accessCredentials = new AccessCredentials(token, null, [...]);

  var authClient = authenticatedClient(client, accessCredentials)
});

I see that this is a bit unconvenient :-/

Does the Chrome Extension Identity API give you an expiry date as well? How do we know for how long the access token will be valid?

mkustermann avatar Dec 03 '14 15:12 mkustermann

Hah, seems we took about the same time to write this up :)

The Chrome Extension Identity API only returns a token without any extra information. The suggested approach there is to just call chrome.identity.getAuthToken() each time you need a token, since it's a relatively low-cost operation that will take care of refreshing the token when necessary. So in this context the expiry date isn't really a necessary information. Neither are the scopes since you would request them beforehand. (I might be missing something but are the scopes in AccessCredentials actually used for something?)

See: https://developer.chrome.com/apps/identity#method-getAuthToken "The Identity API caches access tokens in memory, so it's ok to call getAuthToken non-interactively any time a token is required. The token cache automatically handles expiration."

Scarygami avatar Dec 03 '14 15:12 Scarygami

Thank you both.

But Scarygami, I don't find BrowserClient class... And mkustermann, your code throws an exception because of mirror...

:cry:

tristancaron avatar Dec 03 '14 16:12 tristancaron

It's in package:http/browesr_client.dart.

So don't use new http.Client() but rather new BrowserClient() :)

mkustermann avatar Dec 03 '14 16:12 mkustermann

Yep, better :+1:

And what about this message when I use dart2js

****************************************************************
* WARNING: dart:mirrors support in dart2js is experimental,
*          and not recommended.
*          This implementation of mirrors is incomplete,
*          and often greatly increases the size of the generated
*          JavaScript code.
*
* Your app imports dart:mirrors via:
*   main.dart => package:googleapis => package:http => dart:mirrors
*   main.dart => package:googleapis_auth => package:http => dart:mirrors
*
* Starting with Dart 1.9, you must use the
* --enable-experimental-mirrors command-line flag to opt-in.
* You can begin using this flag now if mirrors support is critical.
*
* To learn what to do next, please visit:
*    http://dartlang.org/dart2js-reflection
****************************************************************

tristancaron avatar Dec 03 '14 16:12 tristancaron