モバオペ API
概要
モバオペAPIは、各種デバイスの予約と解放を可能にするRESTful APIです。 モバオペ利用画面よりアクセストークンを発行し、APIベースでadb coonect urlを取得し、利用可能にするためのサービスです。
APIs
コード例
認証について
モバオペは認証にOAuth 2.0を使用します。 APIを使用するには、まずアクセストークンを生成する必要があります。 アクセストークンは、モバオペ画面から簡単に生成できます。
[設定]タブに移動し、[認証キー]セクションに新しいアクセストークンを生成するだけです。 一度生成したトークンは必ず保存してください。
忘れた場合は一度削除し、新しくトークンを生成する必要があります。
(1) Using cURL
curl -H "Authorization: Bearer YOUR-TOKEN-HERE" \
(2) Using Node.js
var Swagger = require('swagger-client');
var SWAGGER_URL = 'https://app.mbop.io/api/v1/swagger.json';
var AUTH_TOKEN = 'xx-xxxx-xx';
// Without Promise
var client = new Swagger({
url: SWAGGER_URL
, authorizations: {
accessTokenAuth: new Swagger.ApiKeyAuthorization(
'Authorization',
'Bearer ' + AUTH_TOKEN, 'header'
)
}
, success: function() {
client.user.getUser(function(user) {
console.log(user.obj)
})
}
});
// Using Promise
var clientWithPromise = new Swagger({
url: SWAGGER_URL
, usePromise: true
, authorizations: {
accessTokenAuth:
new Swagger.ApiKeyAuthorization(
'Authorization',
'Bearer ' + AUTH_TOKEN, 'header'
)
}
})
clientWithPromise.then(function(api) {
api.user.getUser()
.then(function(res) {
console.log(res.obj.user.email)
})
})
(3) jq コマンドでのjsonレスポンス確認
curl -H "Authorization: Bearer YOUR-TOKEN-HERE" \
https://app.mbop.io/api/v1/devices | jq .
端末(スマホ)について
GET /devices
デバイスリストの取得
GET /api/v1/devices
(1) Using cURL
curl -H "Authorization: Bearer YOUR-TOKEN-HERE" \
(2) Using Node.js
clientWithPromise.then(function(api) {
api.devices.getDevices()
.then(function(res) {
console.log(res.obj.devices.length)
})
})
// OR
clientWithPromise.then(function(api) {
api.devices.getDevices({fields: 'serial,using,ready'})
.then(function(res) {
console.log(res.obj.devices)
})
})
GET /devices/{serial}
デバイスに関する情報を返します
GET /api/v1/devices/{serial}
(1) Using cURL
curl -H "Authorization: Bearer YOUR-TOKEN-HERE" \
(2) Using Node.js
clientWithPromise.then(function(api) {
api.devices.getDeviceBySerial({serial: 'xxxx'})
.then(function(res) {
console.log(res.obj.device.serial)
// xxxx
})
})
// OR
clientWithPromise.then(function(api) {
api.devices.getDeviceBySerial(
{
serial: 'xxxx',
fields: 'serial,
using,ready'
})
.then(function(res) {
console.log(res.obj.device)
// { serial: 'xxxx', using: false, ready: true }
})
})
ユーザー情報について
GET /user
GET /api/v1/user
(1)Using cURL
curl -H "Authorization: Bearer YOUR-TOKEN-HERE" \
(2)Using Node.js
clientWithPromise.then(function(api) {
api.user.getUser()
.then(function(res) {
console.log(res.obj.user.email)
})
})
GET /user/devices
ユーザー(自身)が利用している端末情報を返します。
GET /api/v1/user/devices
(1)Using cURL
curl -H "Authorization: Bearer YOUR-TOKEN-HERE" \
(2)Using Node.js
var device = {serial: 'yyyy', timeout: 900000 }
clientWithPromise.then(function(api) {
return api.user.addUserDevice({device: device})
.then(function(res) {
console.log(res.obj)
})
})
.catch(function(err) {
console.log(err)
})
DELETE /user/devices/{serial}
ユーザーのデバイスリストからデバイスを削除します。 これは、モバオペ画面内でので「停止」を押すことと同じ内容です。
(1)Using cURL
curl -X DELETE -H "Authorization: Bearer YOUR-TOKEN-HERE" \
(2)Using Node.js
clientWithPromise.then(function(api) {
return api.user.deleteUserDeviceBySerial(
{serial: 'yyyy'}
)
.then(function(res) {
console.log(res.obj)
})
})
.catch(function(err) {
console.log(err)
})
POST /user/devices/{serial}/remoteConnect
認証がされた後実行することにより、選択した端末からリモートデバッグURL(adb接続)を取得することが出来ます。
※但し初めてadb接続する端末に関してはモバオペの対象端末で許可画面のアラートが出ますので、それを許可することが必要です。こちらは最初の一回のみです。
POST /api/v1/user/devices/{serial}/remoteConnect
(1) Using cURL
curl -X POST -H "Authorization: Bearer YOUR-TOKEN-HERE" \
(2) Using Node.js
clientWithPromise.then(function(api) {
return api.user.remoteConnectUserDeviceBySerial(
{serial: 'CB5125LBYM'}
)
.then(function(res) {
console.log(res.obj.remoteConnectUrl)
})
})
.catch(function(err) {
console.log(err)
})
サンプル
Using shell and jq
#!/usr/bin/env bash
set -euxo pipefail
if [ "$DEVICE_SERIAL" == "" ]; then
echo "Please specify device serial using ENV['DEVICE_SERIAL']"
exit 1
fi
if [ "$MBOP_URL" == "" ]; then
echo "Please specify MBOP url using ENV['MBOP_URL']"
exit 1
fi
if [ "$MBOP_TOKEN" == "" ]; then
echo "Please specify MBOP token using using ENV['MBOP_TOKEN']"
exit 1
fi
function add_device
{
response=$(curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $MBOP_TOKEN" \
--data "{\"serial\": \"$DEVICE_SERIAL\"}" \
$MBOP_URL/api/v1/user/devices)
success=$(echo "$response" | jq .success | tr -d '"')
description=$(echo "$response" | jq .description | tr -d '"')
if [ "$success" != "true" ]; then
echo "Failed because $description"
exit 1
fi
echo "Device $DEVICE_SERIAL added successfully"
}
function remote_connect
{
response=$(curl -X POST \
-H "Authorization: Bearer $MBOP_TOKEN" \
$MBOP_URL/api/v1/user/devices/$DEVICE_SERIAL/remoteConnect)
success=$(echo "$response" | jq .success | tr -d '"')
description=$(echo "$response" \
| jq .description | tr -d '"')
if [ "$success" != "true" ]; then
echo "Failed because $description"
exit 1
fi
remote_connect_url=$(echo "$response" \
| jq .remoteConnectUrl | tr -d '"')
adb connect $remote_connect_url
echo "Device $DEVICE_SERIAL remote connected successfully"
}
function remove_device
{
response=$(curl -X DELETE \
-H "Authorization: Bearer $MBOP_TOKEN" \
$MBOP_URL/api/v1/user/devices/$DEVICE_SERIAL)
success=$(echo "$response" | jq .success | tr -d '"')
description=$(echo "$response" \
| jq .description | tr -d '"')
if [ "$success" != "true" ]; then
echo "Failed because $description"
exit 1
fi
echo "Device $DEVICE_SERIAL removed successfully"
}
Using NodeJs
デバイス(スマホ)に接続し、認証URLを取得する
var Swagger = require('swagger-client');
var SWAGGER_URL = 'https://app.mbop.io/api/v1/swagger.json';
var AUTH_TOKEN = 'xx-xxxx-xx';
// Using Promise
var client = new Swagger({
url: SWAGGER_URL
, usePromise: true
, authorizations: {
accessTokenAuth: new Swagger.ApiKeyAuthorization(
'Authorization',
'Bearer ' + AUTH_TOKEN,
'header'
)
}
})
var serial = process.argv.slice(2)[0]
client.then(function(api) {
return api.devices.getDeviceBySerial({
serial: serial
, fields: 'serial,present,ready,using,owner'
}).then(function(res) {
// check if device can be added or not
var device = res.obj.device
if (
!device.present ||
!device.ready ||
device.using ||
device.owner
) {
console.log('Device is not available')
return
}
// add device to user
return api.user.addUserDevice({
device: {
serial: device.serial
, timeout: 900000
}
}).then(function(res) {
if (!res.obj.success) {
console.log('Could not add device')
return
}
// get remote connect url
return api.user.remoteConnectUserDeviceBySerial(
{
serial: device.serial
}
).then(function(res) {
console.log(res.obj.remoteConnectUrl)
})
})
})
})
node stf-disconnect.js xxxx
# Device disconnected successfully!