如何同时使用 Polymer 的 google-client-loader 和 google-signin?
How to use Polymer's google-client-loader and google-signin together?
我想使用 google-signin
对用户进行身份验证,然后能够通过 google-client-loader
向我的端点 API 发出授权的 API 请求。
如何将它们结合起来以实现这一点?
一旦通过 google-signin
元素进行身份验证,所有通过 gapi.client
方法的调用都将自动进行身份验证。
我有一个旧的 Polymer 0.5 示例,它使用 Google API 执行此操作,但同样适用于您自己的端点,并且行为在 Polymer 1.0 中没有太大不同:
https://github.com/Scarygami/google-signin-samples/blob/master/elements/google-plus-friends.html#L57
<google-signin
...
is-authorized="{{_authorized}}">
<google-client-loader
id="your_api"
name="your_api"
version="your_api_version"
app-id="your-appengine-id"
on-google-api-load="_apiLoaded"></google-client-loader>
...
_makeRequest: function () {
if (this._authorized && this.$.your_api.api) {
// authentication has happened and api has been loaded
// this call will automatically get authorization headers
this.$.your_api.api.your_resource.your_method({<params>}).then(...)
}
}
PS。我还创建了一个元素,它会自动为你的所有 API 方法创建元素,这样你就可以以声明方式访问你的 API(将 google-client-loader 包装在里面):https://github.com/Scarygami/discovery-api-elements/
我想使用 google-signin
对用户进行身份验证,然后能够通过 google-client-loader
向我的端点 API 发出授权的 API 请求。
如何将它们结合起来以实现这一点?
一旦通过 google-signin
元素进行身份验证,所有通过 gapi.client
方法的调用都将自动进行身份验证。
我有一个旧的 Polymer 0.5 示例,它使用 Google API 执行此操作,但同样适用于您自己的端点,并且行为在 Polymer 1.0 中没有太大不同:
https://github.com/Scarygami/google-signin-samples/blob/master/elements/google-plus-friends.html#L57
<google-signin
...
is-authorized="{{_authorized}}">
<google-client-loader
id="your_api"
name="your_api"
version="your_api_version"
app-id="your-appengine-id"
on-google-api-load="_apiLoaded"></google-client-loader>
...
_makeRequest: function () {
if (this._authorized && this.$.your_api.api) {
// authentication has happened and api has been loaded
// this call will automatically get authorization headers
this.$.your_api.api.your_resource.your_method({<params>}).then(...)
}
}
PS。我还创建了一个元素,它会自动为你的所有 API 方法创建元素,这样你就可以以声明方式访问你的 API(将 google-client-loader 包装在里面):https://github.com/Scarygami/discovery-api-elements/