6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

KiiCloudのRest APIをnode.jsで取得する方法

Posted at

KiiCloudではRestAPIの取得方法をcurlコマンドで紹介しています。
Kiicloud Rest APIの取得方法
これらをcurlコマンドではなく、node.jsで取得したいと思い、サポートの方に質問してみたところ、本当に手厚い対応をしていただき、感謝のつもりで、Qiitaに投稿します。ほとんどコードはサポートの人が教えてくれました。
コミニティでの回答
大げさのことではなく、基本的にHTTP通信が可能な環境であれば、RestAPIを実行することは可能だそうです。参考になれば幸いです。

##ユーザーの作成 公式ドキュメント

curl
curl -v -X POST \
  -H "X-Kii-AppID: {APP_ID}" \
  -H "X-Kii-AppKey: {APP_KEY}" \
  -H "Content-Type: application/vnd.kii.RegistrationRequest+json" \
  "https://api-jp.kii.com/api/apps/{APP_ID}/users" \
  -d '{"loginName": "user_123456", "displayName": "person test000", "country": "JP", "password": "123ABC"}'
node.js

var username = "user";
var request_body = {
  "loginName":username,
  "displayName":username,
  "country":"JP",
  "password":"password"
};
var json = JSON.stringify(request_body);
var options = {
  hostname: "api-jp.kii.com",
  port: 443,
  path: "/api/apps/[YourAppID]/users",
  method: "POST",
  agent: false,
  headers: {
    "content-length":json.length,
    "content-type":"application/vnd.kii.RegistrationRequest+json",
    "x-kii-appid":"[YourAppID]",
    "x-kii-appkey":"[YourAppKey]"
  }
};
var https = require("https");
var req = https.request(options, function(res) {
  res.on('data', function() {
  });
  res.on('end', function() {
    if (res.statusCode != 201) {
      console.log("failure:" + res.statusCode);
    } else {
      console.log("success");
    }
  });
  res.on('error', function(err) {
    console.log("failure:" + err);
  });
});
req.on('error', function(err) {
  console.log("failure:" + err);
});
req.write(json);
req.end();

##ログイン 公式ドキュメント

curl
curl -v -X POST \
  -H "X-Kii-AppID: {APP_ID}" \
  -H "X-Kii-AppKey: {APP_KEY}" \
  -H "Content-Type: application/json" \
  "https://api-jp.kii.com/api/oauth2/token" \
  -d '{"username": "user_123456", "password": "123ABC"}'
node.js
var username = "name";
var password = "password";
var country = "JP";
var AppID = "任意";
var AppKey = "任意";

var request_body = {
	"username":username,
	"password":password
};
var json = JSON.stringify(request_body);
var options = {
	hostname: "api-jp.kii.com",
	port: 443,
	path: "/api/oauth2/token",
	method: "POST",
	agent: false,
	headers: { "content-length":json.length,
				"content-type":"application/json",
				"x-kii-appid":AppID,
				"x-kii-appkey":AppKey,
				}
			};
			
var https = require("https");
var req = https.request(options, function(res) {
	var body = '';
	res.on('data', function(chunk) {
		body += chunk;
	});
	res.on('end', function() {
			console.log("ログインデータ");
			console.log(body);
	});
	res.on('error', function(err) {
		console.log("error");
		console.log("failure:" + err);
	});
});
req.write(json); 
req.end();
/*
取得データ
"id" : "任意",
"access_token" : "任意",
"expires_in" : 2147483639,
"token_type" : "Bearer"
*/

##オブジェクトの取得 公式ドキュメント

curl
curl -v -X GET \
  -H "Authorization: Bearer {ACCESS_TOKEN}" \
  -H "X-Kii-AppID: {APP_ID}" \
  -H "X-Kii-AppKey: {APP_KEY}" \
  "https://api-jp.kii.com/api/apps/{APP_ID}/users/me/buckets/{BUCKET_ID}/objects/{OBJECT_ID}"
node.js
//例ではアプリケーションスコープのデータ取得
//スコープについては下記 
//http://documentation.kii.com/ja/starts/cloudsdk/cloudoverview/idanduri/

var username = "name";
var password = "password";
var country = "JP";
var AppID = "任意";
var AppKey = "任意";
var accessToken = "上記のログインで取得";
var bakectName = "任意";
var objID = "決め打ち or 検索して取得";

var Authorization = "Bearer " + accessToken;

	var options = {
	hostname: "api-jp.kii.com",
	port: 443,
	path: "/api/apps/"+ AppID +"/buckets/"+ bakectName +"/objects/"+ objID,
	method: "GET",
	agent: false,
	headers: { 
				"content-type":"application/json",
				"x-kii-appid":AppID,
				"x-kii-appkey":AppKey,
				"Authorization": Authorization
				}
			};
			
	var https = require("https");
	var req = https.request(options, function(res) {
		var body = '';
		res.on('data', function(chunk) {
			body += chunk;
		});
		res.on('end', function() {
				console.log(body);
				var data = JSON.parse(body);
                                //dataをごにょごにょ
		});
		res.on('error', function(err) {
			console.log("error");
			console.log("failure:" + err);
		});
	});
	req.end();
/*
取得データ
オブジェクトデータ
*/

以上でございます。KiiCloud万歳。

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?