Angular - 创建服务以使用 google url 缩短器
Angular - creating service to use google url shortener
我正在尝试编写一个使用 google url shortener 的服务,但遇到了问题
以下是我的服务:
urlShortener(longUrl: string): Observable<string> {
let body = {longUrl: longUrl}
let options = {
params: {key: XXXXXX},
};
return this.http.post('https://www.googleapis.com/urlshortener/v1/url', body, options)
.map(response => {
console.debug('response',response);
return response;
})
.catch(this.handleError);
}
来自 google 的错误 API:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "authError",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Invalid Credentials"
}
}
使用 API 密钥没有错误,因为在 angular1 中编写的相同代码正在返回 shortUrl
不确定,但应在授权中提供密钥 header。
示例:
let headers = new Headers();
headers.append("Authorization","Basic YW5ndWxhci13YXJlaG91c2Utc2VydmljZXM6MTIzNDU2");
this.http.post(AUTHENTICATION_ENDPOINT, null, {headers: headers}).subscribe(response => {
console.log(response);
});
折腾了一天,用http.request而不是http.post解决了
代码如下:
let myHeaders = new Headers();
myHeaders.append('Content-Type', 'application/json');
let myParams = new URLSearchParams();
myParams.append('key', 'XXX-XXXX);
const options = new RequestOptions({
method: RequestMethod.Post,
headers: myHeaders,
params: myParams,
url: 'https://www.googleapis.com/urlshortener/v1/url',
body: {longUrl: longUrl}
});
const req = new Request(options);
return this.http.request(req)
.map(response => {
console.debug('response', response.json().id);
return response.json().id;
})
.catch(this.handleError);
我正在尝试编写一个使用 google url shortener 的服务,但遇到了问题 以下是我的服务:
urlShortener(longUrl: string): Observable<string> {
let body = {longUrl: longUrl}
let options = {
params: {key: XXXXXX},
};
return this.http.post('https://www.googleapis.com/urlshortener/v1/url', body, options)
.map(response => {
console.debug('response',response);
return response;
})
.catch(this.handleError);
}
来自 google 的错误 API:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "authError",
"message": "Invalid Credentials",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Invalid Credentials"
}
}
使用 API 密钥没有错误,因为在 angular1 中编写的相同代码正在返回 shortUrl
不确定,但应在授权中提供密钥 header。
示例:
let headers = new Headers();
headers.append("Authorization","Basic YW5ndWxhci13YXJlaG91c2Utc2VydmljZXM6MTIzNDU2");
this.http.post(AUTHENTICATION_ENDPOINT, null, {headers: headers}).subscribe(response => {
console.log(response);
});
折腾了一天,用http.request而不是http.post解决了 代码如下:
let myHeaders = new Headers();
myHeaders.append('Content-Type', 'application/json');
let myParams = new URLSearchParams();
myParams.append('key', 'XXX-XXXX);
const options = new RequestOptions({
method: RequestMethod.Post,
headers: myHeaders,
params: myParams,
url: 'https://www.googleapis.com/urlshortener/v1/url',
body: {longUrl: longUrl}
});
const req = new Request(options);
return this.http.request(req)
.map(response => {
console.debug('response', response.json().id);
return response.json().id;
})
.catch(this.handleError);