サーバー間(信頼できる)通信では、未加工の REST や gRPC で開発するよりも、言語固有の GAPI クライアント ライブラリを使用することをおすすめします。これらのクライアントのベースとなっている protobuf ファイルは、https://github.com/googleapis/googleapis/tree/master/google/maps/fleetengine/delivery/v1 で公開されています。
アプリケーションの言語にライブラリが存在しない場合は、gRPC または Fleet Engine REST エンドポイントを使用することをおすすめします。
注: GAPIC ライブラリは、信頼できる(サーバー)環境で実行することが想定されています。JWT は不要です。アプリケーションのデフォルト認証情報と適切な deliveryAdmin
ロールを使用します。
Java
Java ライブラリは google.maps.fleetengine.delivery.v1
で公開されています。
Gradle
plugins {
id "maven-publish"
id "com.google.cloud.artifactregistry.gradle-plugin" version "2.1.4"
}
publishing {
repositories {
maven {
url "artifactregistry://us-maven.pkg.dev/fleetengine-gapic/maven"
}
}
}
repositories {
maven {
url "artifactregistry://us-maven.pkg.dev/fleetengine-gapic/maven"
}
}
dependencies {
implementation 'com.google.maps:gapic-google-maps-fleetengine-delivery-v1-java:latest.release'
}
Maven
<project>
<distributionManagement>
<snapshotRepository>
<id>artifact-registry</id>
<url>artifactregistry://us-maven.pkg.dev/fleetengine-gapic/maven</url>
</snapshotRepository>
<repository>
<id>artifact-registry</id>
<url>artifactregistry://us-maven.pkg.dev/fleetengine-gapic/maven</url>
</repository>
</distributionManagement>
<repositories>
<repository>
<id>artifact-registry</id>
<url>artifactregistry://us-maven.pkg.dev/fleetengine-gapic/maven</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<build>
<extensions>
<extension>
<groupId>com.google.cloud.artifactregistry</groupId>
<artifactId>artifactregistry-maven-wagon</artifactId>
<version>2.1.4</version>
</extension>
</extensions>
</build>
<dependency>
<groupId>com.google.maps</groupId>
<artifactId>gapic-google-maps-fleetengine-delivery-v1-java</artifactId>
<version>LATEST</version>
</dependency>
</project>
Fleet Engine Auth Library for Java を使用すると、Java 環境内で署名付き JSON ウェブトークンを作成できます。
Fleet Engine API を操作する Java の例は、Fleet Engine スタートガイドのページで確認できます。
Node.js / TypeScript
npm
package.json
の dependencies
セクションでライブラリの URL を指定できます。
{
"dependencies": {
"@googlemaps/fleetengine-delivery": "https://storage.googleapis.com/fleetengine-gapic/dist/latest_release/maps-fleetengine-delivery-v1-nodejs.tar.gz",
"google-auth-library": "^9.2.0",
"googleapis": "^118.0.0"
}
}
サンプルコード:
const {google} = require('googleapis');
const fleetengine = require('@googlemaps/fleetengine-delivery');
const {GoogleAuth} = require('google-auth-library');
// CONSTANTS
const PROJECT_ID = 'YOUR_GCP_PROJECT_NAME';
const VEHICLE_ID = 'YOUR_VEHICLE_ID';
const SERVICE_ACCOUNT = 'YOUR_SERVICE_ACCOUNT';
const SERVICE_ACCOUNT_EMAIL = `${SERVICE_ACCOUNT}@${PROJECT_ID}.iam.gserviceaccount.com`;
// CREATE A JWT FOR AUTHENTICATION
const HOWLONG = 55 * 60; // 55 minutes, cannot be more than 60 minutes
async function signToken(claims) {
const googleAuth = new google.auth.GoogleAuth({
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
});
const authClient = await googleAuth.getClient();
google.options({auth: authClient});
const now = Math.round(Date.now() / 1000);
const iat = now - 300;
const exp = now + HOWLONG;
const request = {
name: `projects/-/serviceAccounts/${SERVICE_ACCOUNT_EMAIL}`,
requestBody: {
payload: JSON.stringify({
iss: SERVICE_ACCOUNT_EMAIL,
sub: SERVICE_ACCOUNT_EMAIL,
aud: 'https://fleetengine.googleapis.com/',
iat: iat,
exp: exp,
authorization: claims
}),
}
};
const response = await google.iamcredentials('v1').projects.serviceAccounts
.signJwt(request)
.catch((err) => {
if (err.errors) throw err.errors;
else throw err;
});
return response.data.signedJwt;
}
// MAKE A REQUEST
async function main() {
const claims = {
deliveryvehicleid: VEHICLE_ID
};
signToken(claims).then(token => {
let auth = new GoogleAuth();
auth.cachedCredential = new AuthorizationHeaderProvider(token);
const client = new fleetengine.DeliveryServiceClient({ auth: auth });
client.getDeliveryVehicle({name: `providers/${PROJECT_ID}/deliveryVehicles/${VEHICLE_ID}`}).then(function(resp) {
console.log(resp);
}, function(err) {
console.log(err);
});
});
}
class AuthorizationHeaderProvider {
constructor(token) {
this.token = token;
}
getRequestMetadata(url, callback) {
callback(null, {'authorization': `Bearer ${this.token}`});
}
}
main().catch(console.error);
Go
Go ライブラリは、https://pkg.go.dev/cloud.google.com/go/maps でモジュールとしてパッケージ化されています。
Python
https://pypi.org/project/google-maps-fleetengine-delivery/0.1.0/ をご覧ください。
pip
pip install google-auth
pip install google-maps-fleetengine-delivery
C#
C# ライブラリのインストール手順については、https://www.nuget.org/packages/Google.Maps.FleetEngine.Delivery.V1 をご覧ください。
PHP
PHP ライブラリは https://storage.googleapis.com/fleetengine-gapic/dist/latest_release/google-maps-fleetengine-delivery-v1-php.tar.gz からダウンロードできます。
Ruby
https://rubygems.org/gems/google-maps-fleet_engine-delivery をご覧ください。