そのため、Angular アプリで Google の URL 短縮機能を使用しましたが、API キーを使用しているため、Angular を使用しているサーバー側で Google API 呼び出しを行う方がスマートで安全だと思いました。
$http
Angularでは非常に簡単な投稿が見つかりましたが、Nodeではnpmパッケージを使用する方がよいことにすぐに気付きましたrequest
が、機能していないようです。
だから本質的に私はする必要があります:
POST https://www.googleapis.com/urlshortener/v1/url
Content-Type: application/json
{"longUrl": "http://www.google.com/"}
そして、私は現在書いています:
//Load the request module
var request = require('request');
//Configure and make the request
request({
url: 'https://www.googleapis.com/urlshortener/v1/url?key=XXXXXXXXX',
method: 'POST',
headers: { //We can define headers too
'Content-Type': 'application/json'
},
data: {
'longUrl': 'http://www.myverylongurl.com'
}
}, function(error, response, body){
if(error) {
console.log(error);
} else {
console.log(response.statusCode, response.body);
}
});
エラーが発生し続けます:
"errors": [{ "domain": "global", "reason": "required", "message": "Required", "locationType": "parameter”, “location": "resource.longUrl"
}]
私の要求は間違っていますか?
ありがとう。