【发布时间】:2017-09-11 13:35:58
【问题描述】:
我目前在使用 google url 缩短器时遇到问题。 我已经设置了这个服务:
angular.module('widget.core').service('urlShortener', service);
function service($log, $q, $http) {
var gapiKey = '<MyApiKey>';
var gapiUrl = 'https://www.googleapis.com/urlshortener/v1/url';
return {
shorten: shorten
};
//////////////////////////////////////////////////
function shorten(url) {
console.log(url);
var data = {
method: 'POST',
url: gapiUrl + '?key=' + gapiKey,
headers: {
'Content-Type': 'application/json',
},
data: {
longUrl: url,
}
};
return $http(data).then(function (response) {
$log.debug(response);
return response.data;
}, function (response) {
$log.debug(response);
return response.data;
});
};
};
据我所知,这应该可行。我输入了正确的 API 密钥,当我运行这个方法时,我得到了这个错误:
{
error: {
code: 401,
message: 'Invalid credentials'
}
}
但是,如果我使用邮递员并完全按照这种方法进行设置:
- 发布
- 添加 content-type 标头并将其设置为 application/json
- 将网址设置为https://www.googleapis.com/urlshortener/v1/url?key=myapikey
-
将正文设置为:
{ longUrl: 'myreallylogurl.com' }
当我发布此内容时,它可以正常工作。 我在 google 控制台上检查了我的应用程序,它肯定设置为无限制。
以前有人遇到过这个问题吗?有人知道怎么解决吗?
【问题讨论】:
标签: javascript angularjs google-url-shortener