Skip to content
This repository has been archived by the owner on Jan 26, 2021. It is now read-only.

clientViaToken support? #10

Open
tristancaron opened this issue Dec 3, 2014 · 6 comments
Open

clientViaToken support? #10

tristancaron opened this issue Dec 3, 2014 · 6 comments

Comments

@tristancaron
Copy link

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);
      // ...
    });
});
@Scarygami
Copy link
Contributor

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

@mkustermann
Copy link
Contributor

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?

@Scarygami
Copy link
Contributor

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."

@tristancaron
Copy link
Author

Thank you both.

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

😢

@mkustermann
Copy link
Contributor

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

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

@tristancaron
Copy link
Author

Yep, better 👍

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
****************************************************************

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

3 participants