无法使用聚合物中的 Iron-ajax 访问 Google Rest API
Cannot access Google Rest APIs with Iron-ajax in polymer
我正在使用 Polymerfire 进行授权。响应是来自 google REST API 的 401...
你能用iron-ajax提出Googleapi请求吗?它在 oauth 游乐场中运行良好...
代码:
<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="../bower_components/polymerfire/polymerfire.html">
<link rel="import" href="../bower_components/iron-image/iron-image.html">
<link rel="import" href="../bower_components/iron-list/iron-list.html">
<link rel="import" href="../bower_components/google-apis/google-apis.html">
<link rel="import" href="../bower_components/iron-ajax/iron-ajax.html">
<link rel="import" href="shared-styles.html">
<dom-module id="doc-create">
<template>
<style include="shared-styles">
:host {
display: block;
padding: 10px;
}
</style>
<!--
<iron-ajax
auto
url="https://api.github.com/repos/firebase/polymerfire/issues"
handle-as="json"
params="{state: "closed", page: "1"}"
last-response="{{ajaxResponse}}"></iron-ajax> -->
<iron-ajax
auto
url="https://www.googleapis.com/drive/v3/about"
params = "{{ajaxParams}}"
handle-as="json"
with-credentials
last-response="{{ajaxResponse}}"></iron-ajax>
<div class="card">
<template is="dom-repeat" items="[[ajaxResponse]]">
<div class="horizontal-section">
<p>[[index]]: [[item.title]]</p>
</div>
</template>
</div>
<div class="card">
Response Data: [[ajaxResponse]]
Params: [[ajaxParams.fields]]
</div>
</template>
<script>
Polymer({
is: 'doc-create',
properties: {
fields: {
type: String,
value: 'user'
},
apikey: {
type: String,
value: 'ya29.CjBVA-xV9TJ9cS25hx9qJvEgD1w'
},
ajaxParams: {
type: String,
computed: 'processParams(fields, apikey)'
}
},
processParams: function(fields, apikey) {
return {
fields: fields,
key: apikey
};
}
// ,
// ready: function(){
// var request = api.url.get({
// shortUrl: 'oo.gl/fbsS'
// });
// request.execute(function(resp) {
// console.log(resp);
// });
// }
});
</script>
</dom-module>
我做错了什么???
Screeshootof console
TL;DR - 您没有正确授权该请求。您需要正确提供访问令牌,而不是 API 密钥。
401 错误通常是授权错误,意味着没有访问令牌或范围不足。虽然我对 iron-ajax 不太熟悉,但样本中有一个奇怪的地方。
apiKey 值似乎是访问令牌,而不是 API 密钥(基于 ya29 前缀。)如果您希望它成为访问令牌,则应通过授权传递 header (Authorization: Bearer your_token_value
) 或通过名为 access_token
.
的查询参数
如果您打算将其作为 API 键,请注意 API 键在这种情况下是不够的。特定的 API 对用户数据进行操作,需要用户授权的 Outh2 访问令牌。 API 密钥未绑定到用户且未授予必要的权限。
此处解决:
https://github.com/firebase/polymerfire/issues/108
使用然后授权使用 "signinwithcreditials"。
我正在使用 Polymerfire 进行授权。响应是来自 google REST API 的 401...
你能用iron-ajax提出Googleapi请求吗?它在 oauth 游乐场中运行良好...
代码:
<link rel="import" href="../bower_components/polymer/polymer.html">
<link rel="import" href="../bower_components/polymerfire/polymerfire.html">
<link rel="import" href="../bower_components/iron-image/iron-image.html">
<link rel="import" href="../bower_components/iron-list/iron-list.html">
<link rel="import" href="../bower_components/google-apis/google-apis.html">
<link rel="import" href="../bower_components/iron-ajax/iron-ajax.html">
<link rel="import" href="shared-styles.html">
<dom-module id="doc-create">
<template>
<style include="shared-styles">
:host {
display: block;
padding: 10px;
}
</style>
<!--
<iron-ajax
auto
url="https://api.github.com/repos/firebase/polymerfire/issues"
handle-as="json"
params="{state: "closed", page: "1"}"
last-response="{{ajaxResponse}}"></iron-ajax> -->
<iron-ajax
auto
url="https://www.googleapis.com/drive/v3/about"
params = "{{ajaxParams}}"
handle-as="json"
with-credentials
last-response="{{ajaxResponse}}"></iron-ajax>
<div class="card">
<template is="dom-repeat" items="[[ajaxResponse]]">
<div class="horizontal-section">
<p>[[index]]: [[item.title]]</p>
</div>
</template>
</div>
<div class="card">
Response Data: [[ajaxResponse]]
Params: [[ajaxParams.fields]]
</div>
</template>
<script>
Polymer({
is: 'doc-create',
properties: {
fields: {
type: String,
value: 'user'
},
apikey: {
type: String,
value: 'ya29.CjBVA-xV9TJ9cS25hx9qJvEgD1w'
},
ajaxParams: {
type: String,
computed: 'processParams(fields, apikey)'
}
},
processParams: function(fields, apikey) {
return {
fields: fields,
key: apikey
};
}
// ,
// ready: function(){
// var request = api.url.get({
// shortUrl: 'oo.gl/fbsS'
// });
// request.execute(function(resp) {
// console.log(resp);
// });
// }
});
</script>
</dom-module>
我做错了什么??? Screeshootof console
TL;DR - 您没有正确授权该请求。您需要正确提供访问令牌,而不是 API 密钥。
401 错误通常是授权错误,意味着没有访问令牌或范围不足。虽然我对 iron-ajax 不太熟悉,但样本中有一个奇怪的地方。
apiKey 值似乎是访问令牌,而不是 API 密钥(基于 ya29 前缀。)如果您希望它成为访问令牌,则应通过授权传递 header (Authorization: Bearer your_token_value
) 或通过名为 access_token
.
如果您打算将其作为 API 键,请注意 API 键在这种情况下是不够的。特定的 API 对用户数据进行操作,需要用户授权的 Outh2 访问令牌。 API 密钥未绑定到用户且未授予必要的权限。
此处解决: https://github.com/firebase/polymerfire/issues/108
使用然后授权使用 "signinwithcreditials"。