整合適用於 Gremlin 的 Azure Cosmos DB 與服務 連線 or
本文內容
此頁面會顯示支持的驗證方法和用戶端,並顯示您可以使用Service 連線 or 將 Azure Cosmos DB for Apache Gremlin 連線到其他雲端服務的範例程序代碼。 您仍然可以使用其他程式設計語言連線到適用於 Gremlin 的 Azure Cosmos DB,而不需要使用 Service 連線 or。 此頁面也會顯示您在建立服務連線時取得的預設環境變數名稱和值。
支援的計算服務
服務 連線 or 可用來將下列計算服務連線至適用於 Apache Gremlin 的 Azure Cosmos DB:
Azure App Service
Azure Functions
Azure 容器應用程式
Azure Spring Apps
支援的驗證類型和客戶端類型
下表顯示使用 Service 連線 or 將計算服務連線至適用於 Apache Gremlin 的 Azure Cosmos DB 時,支援用戶端類型和驗證方法的組合。 「是」表示支持組合,而「否」則表示不支援。
用戶端類型
系統指派的受控識別
使用者指派的受控識別
秘密 / 連接字串
服務主體
.NET
Yes
.是
.是
Yes
Java
Yes
.是
.是
Yes
Node.js
Yes
.是
.是
Yes
PHP
Yes
.是
.是
Yes
Python
Yes
.是
.是
Yes
Go
Yes
.是
.是
Yes
無
Yes
.是
.是
Yes
下表指出支持數據表中所有用戶端類型和驗證方法的組合。 所有客戶端類型都可以使用任何驗證方法,使用 Service 連線 or 連線到適用於 Apache Gremlin 的 Azure Cosmos DB。
默認環境變數名稱或應用程式屬性和範例程序代碼
使用下列連線詳細數據,將計算服務連線到適用於Apache Gremlin的 Azure Cosmos DB。 針對下列每個範例,請將佔位元文字 <Azure-Cosmos-DB-account>
、<database>
、<collection or graphs>
、、、<resource-group-name>
<client-secret>
<client-ID>
<password>
<username>
<subscription-ID>
、和 <tenant-id>
取代為您自己的資訊。 如需命名慣例的詳細資訊,請參閱 Service 連線 or internals 一文。
系統指派的受控識別
默認環境變數名稱
描述
範例值
AZURE_COSMOS_LISTKEYURL
要取得 連接字串的 URL
https://management.azure.com/subscriptions/<subscription-ID>/resourceGroups/<resource-group-name>/providers/Microsoft.DocumentDB/databaseAccounts/<Azure-Cosmos-DB-account>/listKeys?api-version=2021-04-15
AZURE_COSMOS_SCOPE
您的受控識別範圍
https://management.azure.com/.default
AZURE_COSMOS_RESOURCEENDPOINT
您的資源端點
https://<Azure-Cosmos-DB-account>.documents.azure.com:443/
AZURE_COSMOS_HOSTNAME
您的 Gremlin 唯一資源識別符 (UFI)
<Azure-Cosmos-DB-account>.gremlin.cosmos.azure.com
AZURE_COSMOS_PORT
連線 ion 埠
443
AZURE_COSMOS_USERNAME
您的用戶名稱
/dbs/<database>/colls/<collection or graphs>
範例指令碼
請參閱下列步驟和程序代碼,以使用系統指派的受控識別連線至適用於 Gremlin 的 Azure Cosmos DB。
安裝相依性。
dotnet add package Gremlin.Net
dotnet add package Azure.Identity
使用客戶端連結庫 Azure.Identity 取得受控識別或服務主體的存取令牌。 使用存取令牌並 AZURE_COSMOS_LISTKEYURL
取得密碼。 從 Service 連線 or 所新增的環境變數取得連線資訊,並連線到適用於 Apache Gremlin 的 Azure Cosmos DB。 使用下列程式代碼時,請取消批註您想要使用的驗證類型代碼段部分。
using System;
using System.Security.Authentication;
using System.Net.Security;
using System.Net.Http;
using System.Security.Authentication;
using System.Threading.Tasks;
using System;
using Gremlin.Net.Driver;
using Azure.Identity;
var gremlinEndpoint = Environment.GetEnvironmentVariable("AZURE_COSMOS_RESOURCEENDPOINT");
var userName = Environment.GetEnvironmentVariable("AZURE_COSMOS_USERNAME");
var gremlinPort = Int32.Parse(Environment.GetEnvironmentVariable("AZURE_COSMOS_PORT"));
var listKeyUrl = Environment.GetEnvironmentVariable("AZURE_COSMOS_LISTKEYURL");
var scope = Environment.GetEnvironmentVariable("AZURE_COSMOS_SCOPE");
// Uncomment the following lines according to the authentication type.
// For system-assigned identity.
// var tokenProvider = new DefaultAzureCredential();
// For user-assigned identity.
// var tokenProvider = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTID");
// }
// );
// For service principal.
// var tenantId = Environment.GetEnvironmentVariable("AZURE_COSMOS_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTSECRET");
// var tokenProvider = new ClientSecretCredential(tenantId, clientId, clientSecret);
// Acquire the access token.
AccessToken accessToken = await tokenProvider.GetTokenAsync(
new TokenRequestContext(scopes: new string[]{ scope }));
// Get the password.
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken.Token}");
var response = await httpClient.POSTAsync(listKeyUrl);
var responseBody = await response.Content.ReadAsStringAsync();
var keys = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseBody);
var password = keys["primaryMasterKey"];
// Connect to Azure Cosmos DB for Apache Gremlin
var server = new GremlinServer(
hostname: gremlinEndpoint,
port: gremlinPort,
username: userName,
password: password,
enableSsl: true
);
using var client = new GremlinClient(
gremlinServer: server,
messageSerializer: new Gremlin.Net.Structure.IO.GraphSON.GraphSON2MessageSerializer()
);
在您的pom.xml檔案中 新增下列相依性:
<dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>gremlin-driver</artifactId>
<version>3.4.13</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.1.5</version>
</dependency>
使用 azure-identity
取得受控識別或服務主體的存取令牌。 使用存取令牌並 AZURE_COSMOS_LISTKEYURL
取得密碼。 從 Service 連線 or 所新增的環境變數取得連線資訊,並連線到適用於 Apache Gremlin 的 Azure Cosmos DB。 使用下列程式代碼時,請取消批註您想要使用的驗證類型代碼段部分。
import org.apache.tinkerpop.gremlin.driver.Client;
import org.apache.tinkerpop.gremlin.driver.Cluster;
import javax.net.ssl.*;
import java.net.InetSocketAddress;
import com.azure.identity.*;
import com.azure.core.credentital.*;
import java.net.http.*;
import java.net.URI;
int gremlinPort = Integer.parseInt(System.getenv("AZURE_COSMOS_PORT"));
String username = System.getenv("AZURE_COSMOS_USERNAME");
String gremlinEndpoint = System.getenv("AZURE_COSMOS_RESOURCEENDPOINT");
String listKeyUrl = System.getenv("AZURE_COSMOS_LISTKEYURL");
String scope = System.getenv("AZURE_COSMOS_SCOPE");
// Uncomment the following lines according to the authentication type.
// For system managed identity.
// DefaultAzureCredential defaultCredential = new DefaultAzureCredentialBuilder().build();
// For user assigned managed identity.
// DefaultAzureCredential defaultCredential = new DefaultAzureCredentialBuilder()
// .managedIdentityClientId(System.getenv("AZURE_COSMOS_CLIENTID"))
// .build();
// For service principal.
// ClientSecretCredential defaultCredential = new ClientSecretCredentialBuilder()
// .clientId(System.getenv("AZURE_COSMOS_CLIENTID"))
// .clientSecret(System.getenv("AZURE_COSMOS_CLIENTSECRET"))
// .tenantId(System.getenv("AZURE_COSMOS_TENANTID"))
// .build();
// Get the access token.
AccessToken accessToken = defaultCredential.getToken(new TokenRequestContext().addScopes(new String[]{ scope })).block();
String token = accessToken.getToken();
// Get the password.
HttpClient client = HttpClient.newBuilder().build();
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(listKeyUrl))
.header("Authorization", "Bearer " + token)
.POST()
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
JSONParser parser = new JSONParser();
JSONObject responseBody = parser.parse(response.body());
String gremlinPassword = responseBody.get("primaryMasterKey");
// Connect to Azure Cosmos DB for Apache Gremlin
Cluster cluster;
Client client;
cluster = Cluster.addContactPoint(gremlinEndpoint).port(gremlinPort).credentials(username, password).create();
安裝相依性。
pip install gremlinpython
使用 azure-identity
來向受控識別或服務主體進行驗證,並傳送要求以 AZURE_COSMOS_LISTKEYURL
取得密碼。 從 Service 連線 or 所新增的環境變數取得連線資訊,並連線到適用於 Apache Gremlin 的 Azure Cosmos DB。 使用下列程式代碼時,請取消批註您想要使用的驗證類型代碼段部分。
from gremlin_python.driver import client, serializer
import requests
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
username = os.getenv('AZURE_COSMOS_USERNAME')
endpoint = os.getenv('AZURE_COSMOS_RESOURCEENDPOINT')
port = os.getenv('AZURE_COSMOS_PORT')
listKeyUrl = os.getenv('AZURE_COSMOS_LISTKEYURL')
scope = os.getenv('AZURE_COSMOS_SCOPE')
# Uncomment the following lines according to the authentication type.
# For system-assigned managed identity
# cred = ManagedIdentityCredential()
# For user-assigned managed identity
# managed_identity_client_id = os.getenv('AZURE_COSMOS_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# For service principal
# tenant_id = os.getenv('AZURE_COSMOS_TENANTID')
# client_id = os.getenv('AZURE_COSMOS_CLIENTID')
# client_secret = os.getenv('AZURE_COSMOS_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
# Get the password
session = requests.Session()
token = cred.get_token(scope)
response = session.post(listKeyUrl, headers={"Authorization": "Bearer {}".format(token.token)})
keys_dict = response.json()
password = keys_dict['primaryMasterKey']
# Connect to Azure Cosmos DB for Apache Gremlin.
client = client.Client(
url=endpoint,
traversal_source="g",
username=username,
password=password,
message_serializer=serializer.GraphSONSerializersV2d0(),
)
安裝相依性。
go get github.com/Azure/azure-sdk-for-go/sdk/azidentity
go get github.com/go-gremlin/gremlin
在程式代碼中,使用 azidentity
取得存取令牌,然後使用它來取得密碼。 從 Service 連線 or 所新增的環境變數取得連線資訊,並連線到適用於 Apache Gremlin 的 Azure Cosmos DB。 使用下列程式代碼時,請取消批註您想要使用的驗證類型代碼段部分。
import (
"fmt"
"os"
"context"
"log"
"io/ioutil"
"encoding/json"
"github.com/go-gremlin/gremlin"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
)
func main() {
username = os.Getenv("AZURE_COSMOS_USERNAME")
endpoint = os.getenv("AZURE_COSMOS_RESOURCEENDPOINT")
port = os.getenv("AZURE_COSMOS_PORT")
listKeyUrl = os.Getenv("AZURE_COSMOS_LISTKEYURL")
scope = os.Getenv("AZUE_COSMOS_SCOPE")
// Uncomment the following lines according to the authentication type.
// For system-assigned identity.
// cred, err := azidentity.NewDefaultAzureCredential(nil)
// For user-assigned identity.
// clientid := os.Getenv("AZURE_COSMOS_CLIENTID")
// azidentity.ManagedIdentityCredentialOptions.ID := clientid
// options := &azidentity.ManagedIdentityCredentialOptions{ID: clientid}
// cred, err := azidentity.NewManagedIdentityCredential(options)
// For service principal.
// clientid := os.Getenv("AZURE_COSMOS_CLIENTID")
// tenantid := os.Getenv("AZURE_COSMOS_TENANTID")
// clientsecret := os.Getenv("AZURE_COSMOS_CLIENTSECRET")
// cred, err := azidentity.NewClientSecretCredential(tenantid, clientid, clientsecret, &azidentity.ClientSecretCredentialOptions{})
// Acquire the access token.
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
token, err := cred.GetToken(ctx, policy.TokenRequestOptions{
Scopes: []string{scope},
})
// Acquire the connection string.
client := &http.Client{}
req, err := http.NewRequest("POST", listKeyUrl, nil)
req.Header.Add("Authorization", "Bearer " + token.Token)
resp, err := client.Do(req)
body, err := ioutil.ReadAll(resp.Body)
var result map[string]interface{}
json.Unmarshal(body, &result)
password, err := result["primaryMasterKey"];
auth := gremlin.OptAuthUserPass(username, password)
client, err := gremlin.NewClient(endpoint, auth)
}
安裝相依性。
npm install gremlin
npm install --save @azure/identity
在程式代碼中,使用 @azure/identity
取得存取令牌,然後使用它來取得密碼。 從 Service 連線 or 所新增的環境變數取得連線資訊,並連線到適用於 Apache Gremlin 的 Azure Cosmos DB。 使用下列程式代碼時,請取消批註您想要使用的驗證類型代碼段部分。
import { DefaultAzureCredential,ClientSecretCredential } from "@azure/identity";
import gremlin from 'gremlin'
const axios = require('axios');
let username = process.env.AZURE_COSMOS_USERNAME;
let endoint = process.env.AZURE_COSMOS_RESOURCEENDPOINT;
let port = process.env.AZURE_COSMOS_PORT;
let listKeyUrl = process.env.AZURE_COSMOS_LISTKEYURL;
let scope = process.env.AZURE_COSMOS_SCOPE;
// Uncomment the following lines according to the authentication type.
// For system-assigned identity.
// const credential = new DefaultAzureCredential();
// For user-assigned identity.
// const clientId = process.env.AZURE_COSMOS_CLIENTID;
// const credential = new DefaultAzureCredential({
// managedIdentityClientId: clientId
// });
// For service principal.
// const tenantId = process.env.AZURE_COSMOS_TENANTID;
// const clientId = process.env.AZURE_COSMOS_CLIENTID;
// const clientSecret = process.env.AZURE_COSMOS_CLIENTSECRET;
// Acquire the access token.
var accessToken = await credential.getToken(scope);
// Get the password.
const config = {
method: 'post',
url: listKeyUrl,
headers: {
'Authorization': `Bearer ${accessToken.token}`
}
};
const response = await axios(config);
const keysDict = response.data;
const password = keysDict['primaryMasterKey'];
const credentials = new gremlin.driver.auth.PlainTextSaslAuthenticator(
username,
password
)
const client = new gremlin.driver.Client(
endpoint,
{
credentials,
traversalsource: 'g',
rejectUnauthorized: true,
mimeType: 'application/vnd.gremlin-v2.0+json'
}
)
client.open()
在上呼叫 REST API AZURE_COSMOS_LISTKEYURL
,以取得具有受控識別或服務主體的存取令牌,以取得 Azure Cosmos DB for Gremlin 的主要密鑰。
$endpoint = getenv('AZURE_COSMOS_RESOURCEENDPOINT');
$username = getenv('AZURE_COSMOS_USERNAME');
$port = getenv('AZURE_COSMOS_PORT');
$db = new Connection([
'host' => $endpoint,
'username' => $username,
'password' => $password,
'port' => $port,
'ssl' => TRUE
]);
使用者指派的受控識別
默認環境變數名稱
描述
範例值
AZURE_COSMOS_LISTKEYURL
要取得 連接字串的 URL
https://management.azure.com/subscriptions/<subscription-ID>/resourceGroups/<resource-group-name>/providers/Microsoft.DocumentDB/databaseAccounts/<Azure-Cosmos-DB-account>/listKeys?api-version=2021-04-15
AZURE_COSMOS_SCOPE
您的受控識別範圍
https://management.azure.com/.default
AZURE_COSMOS_RESOURCEENDPOINT
您的資源端點
https://<Azure-Cosmos-DB-account>.documents.azure.com:443/
AZURE_COSMOS_HOSTNAME
您的 Gremlin 唯一資源識別符 (UFI)
<Azure-Cosmos-DB-account>.gremlin.cosmos.azure.com
AZURE_COSMOS_PORT
連線 ion 埠
443
AZURE_COSMOS_USERNAME
您的用戶名稱
/dbs/<database>/colls/<collection or graphs>
AZURE_CLIENTID
您的用戶端識別碼
<client_ID>
範例指令碼
請參閱下列步驟和程序代碼,以使用使用者指派的受控識別連線到適用於 Gremlin 的 Azure Cosmos DB。
安裝相依性。
dotnet add package Gremlin.Net
dotnet add package Azure.Identity
使用客戶端連結庫 Azure.Identity 取得受控識別或服務主體的存取令牌。 使用存取令牌並 AZURE_COSMOS_LISTKEYURL
取得密碼。 從 Service 連線 or 所新增的環境變數取得連線資訊,並連線到適用於 Apache Gremlin 的 Azure Cosmos DB。 使用下列程式代碼時,請取消批註您想要使用的驗證類型代碼段部分。
using System;
using System.Security.Authentication;
using System.Net.Security;
using System.Net.Http;
using System.Security.Authentication;
using System.Threading.Tasks;
using System;
using Gremlin.Net.Driver;
using Azure.Identity;
var gremlinEndpoint = Environment.GetEnvironmentVariable("AZURE_COSMOS_RESOURCEENDPOINT");
var userName = Environment.GetEnvironmentVariable("AZURE_COSMOS_USERNAME");
var gremlinPort = Int32.Parse(Environment.GetEnvironmentVariable("AZURE_COSMOS_PORT"));
var listKeyUrl = Environment.GetEnvironmentVariable("AZURE_COSMOS_LISTKEYURL");
var scope = Environment.GetEnvironmentVariable("AZURE_COSMOS_SCOPE");
// Uncomment the following lines according to the authentication type.
// For system-assigned identity.
// var tokenProvider = new DefaultAzureCredential();
// For user-assigned identity.
// var tokenProvider = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTID");
// }
// );
// For service principal.
// var tenantId = Environment.GetEnvironmentVariable("AZURE_COSMOS_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTSECRET");
// var tokenProvider = new ClientSecretCredential(tenantId, clientId, clientSecret);
// Acquire the access token.
AccessToken accessToken = await tokenProvider.GetTokenAsync(
new TokenRequestContext(scopes: new string[]{ scope }));
// Get the password.
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken.Token}");
var response = await httpClient.POSTAsync(listKeyUrl);
var responseBody = await response.Content.ReadAsStringAsync();
var keys = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseBody);
var password = keys["primaryMasterKey"];
// Connect to Azure Cosmos DB for Apache Gremlin
var server = new GremlinServer(
hostname: gremlinEndpoint,
port: gremlinPort,
username: userName,
password: password,
enableSsl: true
);
using var client = new GremlinClient(
gremlinServer: server,
messageSerializer: new Gremlin.Net.Structure.IO.GraphSON.GraphSON2MessageSerializer()
);
在您的pom.xml檔案中 新增下列相依性:
<dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>gremlin-driver</artifactId>
<version>3.4.13</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.1.5</version>
</dependency>
使用 azure-identity
取得受控識別或服務主體的存取令牌。 使用存取令牌並 AZURE_COSMOS_LISTKEYURL
取得密碼。 從 Service 連線 or 所新增的環境變數取得連線資訊,並連線到適用於 Apache Gremlin 的 Azure Cosmos DB。 使用下列程式代碼時,請取消批註您想要使用的驗證類型代碼段部分。
import org.apache.tinkerpop.gremlin.driver.Client;
import org.apache.tinkerpop.gremlin.driver.Cluster;
import javax.net.ssl.*;
import java.net.InetSocketAddress;
import com.azure.identity.*;
import com.azure.core.credentital.*;
import java.net.http.*;
import java.net.URI;
int gremlinPort = Integer.parseInt(System.getenv("AZURE_COSMOS_PORT"));
String username = System.getenv("AZURE_COSMOS_USERNAME");
String gremlinEndpoint = System.getenv("AZURE_COSMOS_RESOURCEENDPOINT");
String listKeyUrl = System.getenv("AZURE_COSMOS_LISTKEYURL");
String scope = System.getenv("AZURE_COSMOS_SCOPE");
// Uncomment the following lines according to the authentication type.
// For system managed identity.
// DefaultAzureCredential defaultCredential = new DefaultAzureCredentialBuilder().build();
// For user assigned managed identity.
// DefaultAzureCredential defaultCredential = new DefaultAzureCredentialBuilder()
// .managedIdentityClientId(System.getenv("AZURE_COSMOS_CLIENTID"))
// .build();
// For service principal.
// ClientSecretCredential defaultCredential = new ClientSecretCredentialBuilder()
// .clientId(System.getenv("AZURE_COSMOS_CLIENTID"))
// .clientSecret(System.getenv("AZURE_COSMOS_CLIENTSECRET"))
// .tenantId(System.getenv("AZURE_COSMOS_TENANTID"))
// .build();
// Get the access token.
AccessToken accessToken = defaultCredential.getToken(new TokenRequestContext().addScopes(new String[]{ scope })).block();
String token = accessToken.getToken();
// Get the password.
HttpClient client = HttpClient.newBuilder().build();
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(listKeyUrl))
.header("Authorization", "Bearer " + token)
.POST()
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
JSONParser parser = new JSONParser();
JSONObject responseBody = parser.parse(response.body());
String gremlinPassword = responseBody.get("primaryMasterKey");
// Connect to Azure Cosmos DB for Apache Gremlin
Cluster cluster;
Client client;
cluster = Cluster.addContactPoint(gremlinEndpoint).port(gremlinPort).credentials(username, password).create();
安裝相依性。
pip install gremlinpython
使用 azure-identity
來向受控識別或服務主體進行驗證,並傳送要求以 AZURE_COSMOS_LISTKEYURL
取得密碼。 從 Service 連線 or 所新增的環境變數取得連線資訊,並連線到適用於 Apache Gremlin 的 Azure Cosmos DB。 使用下列程式代碼時,請取消批註您想要使用的驗證類型代碼段部分。
from gremlin_python.driver import client, serializer
import requests
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
username = os.getenv('AZURE_COSMOS_USERNAME')
endpoint = os.getenv('AZURE_COSMOS_RESOURCEENDPOINT')
port = os.getenv('AZURE_COSMOS_PORT')
listKeyUrl = os.getenv('AZURE_COSMOS_LISTKEYURL')
scope = os.getenv('AZURE_COSMOS_SCOPE')
# Uncomment the following lines according to the authentication type.
# For system-assigned managed identity
# cred = ManagedIdentityCredential()
# For user-assigned managed identity
# managed_identity_client_id = os.getenv('AZURE_COSMOS_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# For service principal
# tenant_id = os.getenv('AZURE_COSMOS_TENANTID')
# client_id = os.getenv('AZURE_COSMOS_CLIENTID')
# client_secret = os.getenv('AZURE_COSMOS_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
# Get the password
session = requests.Session()
token = cred.get_token(scope)
response = session.post(listKeyUrl, headers={"Authorization": "Bearer {}".format(token.token)})
keys_dict = response.json()
password = keys_dict['primaryMasterKey']
# Connect to Azure Cosmos DB for Apache Gremlin.
client = client.Client(
url=endpoint,
traversal_source="g",
username=username,
password=password,
message_serializer=serializer.GraphSONSerializersV2d0(),
)
安裝相依性。
go get github.com/Azure/azure-sdk-for-go/sdk/azidentity
go get github.com/go-gremlin/gremlin
在程式代碼中,使用 azidentity
取得存取令牌,然後使用它來取得密碼。 從 Service 連線 or 所新增的環境變數取得連線資訊,並連線到適用於 Apache Gremlin 的 Azure Cosmos DB。 使用下列程式代碼時,請取消批註您想要使用的驗證類型代碼段部分。
import (
"fmt"
"os"
"context"
"log"
"io/ioutil"
"encoding/json"
"github.com/go-gremlin/gremlin"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
)
func main() {
username = os.Getenv("AZURE_COSMOS_USERNAME")
endpoint = os.getenv("AZURE_COSMOS_RESOURCEENDPOINT")
port = os.getenv("AZURE_COSMOS_PORT")
listKeyUrl = os.Getenv("AZURE_COSMOS_LISTKEYURL")
scope = os.Getenv("AZUE_COSMOS_SCOPE")
// Uncomment the following lines according to the authentication type.
// For system-assigned identity.
// cred, err := azidentity.NewDefaultAzureCredential(nil)
// For user-assigned identity.
// clientid := os.Getenv("AZURE_COSMOS_CLIENTID")
// azidentity.ManagedIdentityCredentialOptions.ID := clientid
// options := &azidentity.ManagedIdentityCredentialOptions{ID: clientid}
// cred, err := azidentity.NewManagedIdentityCredential(options)
// For service principal.
// clientid := os.Getenv("AZURE_COSMOS_CLIENTID")
// tenantid := os.Getenv("AZURE_COSMOS_TENANTID")
// clientsecret := os.Getenv("AZURE_COSMOS_CLIENTSECRET")
// cred, err := azidentity.NewClientSecretCredential(tenantid, clientid, clientsecret, &azidentity.ClientSecretCredentialOptions{})
// Acquire the access token.
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
token, err := cred.GetToken(ctx, policy.TokenRequestOptions{
Scopes: []string{scope},
})
// Acquire the connection string.
client := &http.Client{}
req, err := http.NewRequest("POST", listKeyUrl, nil)
req.Header.Add("Authorization", "Bearer " + token.Token)
resp, err := client.Do(req)
body, err := ioutil.ReadAll(resp.Body)
var result map[string]interface{}
json.Unmarshal(body, &result)
password, err := result["primaryMasterKey"];
auth := gremlin.OptAuthUserPass(username, password)
client, err := gremlin.NewClient(endpoint, auth)
}
安裝相依性。
npm install gremlin
npm install --save @azure/identity
在程式代碼中,使用 @azure/identity
取得存取令牌,然後使用它來取得密碼。 從 Service 連線 or 所新增的環境變數取得連線資訊,並連線到適用於 Apache Gremlin 的 Azure Cosmos DB。 使用下列程式代碼時,請取消批註您想要使用的驗證類型代碼段部分。
import { DefaultAzureCredential,ClientSecretCredential } from "@azure/identity";
import gremlin from 'gremlin'
const axios = require('axios');
let username = process.env.AZURE_COSMOS_USERNAME;
let endoint = process.env.AZURE_COSMOS_RESOURCEENDPOINT;
let port = process.env.AZURE_COSMOS_PORT;
let listKeyUrl = process.env.AZURE_COSMOS_LISTKEYURL;
let scope = process.env.AZURE_COSMOS_SCOPE;
// Uncomment the following lines according to the authentication type.
// For system-assigned identity.
// const credential = new DefaultAzureCredential();
// For user-assigned identity.
// const clientId = process.env.AZURE_COSMOS_CLIENTID;
// const credential = new DefaultAzureCredential({
// managedIdentityClientId: clientId
// });
// For service principal.
// const tenantId = process.env.AZURE_COSMOS_TENANTID;
// const clientId = process.env.AZURE_COSMOS_CLIENTID;
// const clientSecret = process.env.AZURE_COSMOS_CLIENTSECRET;
// Acquire the access token.
var accessToken = await credential.getToken(scope);
// Get the password.
const config = {
method: 'post',
url: listKeyUrl,
headers: {
'Authorization': `Bearer ${accessToken.token}`
}
};
const response = await axios(config);
const keysDict = response.data;
const password = keysDict['primaryMasterKey'];
const credentials = new gremlin.driver.auth.PlainTextSaslAuthenticator(
username,
password
)
const client = new gremlin.driver.Client(
endpoint,
{
credentials,
traversalsource: 'g',
rejectUnauthorized: true,
mimeType: 'application/vnd.gremlin-v2.0+json'
}
)
client.open()
在上呼叫 REST API AZURE_COSMOS_LISTKEYURL
,以取得具有受控識別或服務主體的存取令牌,以取得 Azure Cosmos DB for Gremlin 的主要密鑰。
$endpoint = getenv('AZURE_COSMOS_RESOURCEENDPOINT');
$username = getenv('AZURE_COSMOS_USERNAME');
$port = getenv('AZURE_COSMOS_PORT');
$db = new Connection([
'host' => $endpoint,
'username' => $username,
'password' => $password,
'port' => $port,
'ssl' => TRUE
]);
Connection string
默認環境變數名稱
描述
範例值
AZURE_COSMOS_HOSTNAME
您的 Gremlin 唯一資源識別符 (UFI)
<Azure-Cosmos-DB-account>.gremlin.cosmos.azure.com
AZURE_COSMOS_PORT
連線 ion 埠
443
AZURE_COSMOS_USERNAME
您的用戶名稱
/dbs/<database>/colls/<collection or graphs>
AZURE_COSMOS_PASSWORD
您的密碼
<password>
範例指令碼
請參閱下列步驟和程序代碼,以使用 連接字串 連線到適用於 Gremlin 的 Azure Cosmos DB。
安裝相依性。
dotnet add package Gremlin.Net
從 Service 連線 or 所新增的環境變數取得連線資訊,並連線到適用於 Apache Gremlin 的 Azure Cosmos DB。
using System;
using Gremlin.Net.Driver;
var gremlinEndpoint = Environment.GetEnvironmentVariable("AZURE_COSMOS_RESOURCEENDPOINT");
var userName = Environment.GetEnvironmentVariable("AZURE_COSMOS_USERNAME");
var password = Environment.GetEnvironmentVariable("AZURE_COSMOS_PASSWORD");
var gremlinPort = Int32.Parse(Environment.GetEnvironmentVariable("AZURE_COSMOS_PORT"));
var server = new GremlinServer(
hostname: gremlinEndpoint,
port: gremlinPort,
username: userName,
password: password,
enableSsl: true
);
using var client = new GremlinClient(
gremlinServer: server,
messageSerializer: new Gremlin.Net.Structure.IO.GraphSON.GraphSON2MessageSerializer()
);
在您的pom.xml檔案中 新增下列相依性:
<dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>gremlin-driver</artifactId>
<version>3.4.13</version>
</dependency>
從 Service 連線 or 所新增的環境變數取得連線資訊,並連線到適用於 Apache Gremlin 的 Azure Cosmos DB。
import org.apache.tinkerpop.gremlin.driver.Client;
import org.apache.tinkerpop.gremlin.driver.Cluster;
import javax.net.ssl.*;
import java.net.InetSocketAddress;
int gremlinPort = Integer.parseInt(System.getenv("AZURE_COSMOS_PORT"));
String username = System.getenv("AZURE_COSMOS_USERNAME");
String gremlinEndpoint = System.getenv("AZURE_COSMOS_RESOURCEENDPOINT");
String password = System.getenv("AZURE_COSMOS_PASSWORD");
Cluster cluster;
Client client;
cluster = Cluster.addContactPoint(gremlinEndpoint).port(gremlinPort).credentials(username, password).create();
安裝相依性。
pip install gremlinpython
從 Service 連線 or 所新增的環境變數取得連線資訊,並連線到適用於 Apache Gremlin 的 Azure Cosmos DB。
import os
from gremlin_python.driver import client, serializer
username = os.getenv('AZURE_COSMOS_USERNAME')
password = os.getenv('AZURE_COSMOS_PASSWORD')
endpoint = os.getenv('AZURE_COSMOS_RESOURCEENDPOINT')
port = os.getenv('AZURE_COSMOS_PORT')
client = client.Client(
url=endpoint,
traversal_source="g",
username=username,
password=password,
message_serializer=serializer.GraphSONSerializersV2d0(),
)
安裝相依性。
go get github.com/go-gremlin/gremlin
從 Service 連線 or 新增的環境變數取得連線資訊,並連線到適用於 Apache Gremlin 的 Azure Cosmos DB。
username = os.Getenv("AZURE_COSMOS_USERNAME")
password = os.getenv("AZURE_COSMOS_PASSWORD")
endpoint = os.getenv("AZURE_COSMOS_RESOURCEENDPOINT")
port = os.getenv("AZURE_COSMOS_PORT")
auth := gremlin.OptAuthUserPass(username, password)
client, err := gremlin.NewClient(endpoint, auth)
安裝相依性。
npm install gremlin
從 Service 連線 or 所新增的環境變數取得連線資訊,並連線到適用於 Apache Gremlin 的 Azure Cosmos DB。
import gremlin from 'gremlin'
const username = process.env.AZURE_COSMOS_USERNAME;
const password = process.env.AZURE_COSMOS_PASSWORD;
const endpoint = process.env.AZURE_COSMOS_RESOURCEENDPOINT;
const port = process.env.AZURE_COSMOS_PORT;
const credentials = new gremlin.driver.auth.PlainTextSaslAuthenticator(
username,
password
)
const client = new gremlin.driver.Client(
endpoint,
{
credentials,
traversalsource: 'g',
rejectUnauthorized: true,
mimeType: 'application/vnd.gremlin-v2.0+json'
}
)
client.open()
從 Service 連線 or 所新增的環境變數取得連線資訊,並連線到適用於 Apache Gremlin 的 Azure Cosmos DB。
$endpoint = getenv('AZURE_COSMOS_RESOURCEENDPOINT');
$username = getenv('AZURE_COSMOS_USERNAME');
$password = getenv('AZURE_COSMOS_PASSWORD');
$port = getenv('AZURE_COSMOS_PORT');
$db = new Connection([
'host' => $endpoint,
'username' => $username,
'password' => $password,
'port' => $port,
'ssl' => TRUE
]);
服務主體
默認環境變數名稱
描述
範例值
AZURE_COSMOS_LISTKEYURL
要取得 連接字串的 URL
https://management.azure.com/subscriptions/<subscription-ID>/resourceGroups/<resource-group-name>/providers/Microsoft.DocumentDB/databaseAccounts/<Azure-Cosmos-DB-account>/listKeys?api-version=2021-04-15
AZURE_COSMOS_SCOPE
您的受控識別範圍
https://management.azure.com/.default
AZURE_COSMOS_RESOURCEENDPOINT
您的資源端點
https://<Azure-Cosmos-DB-account>.documents.azure.com:443/
AZURE_COSMOS_HOSTNAME
您的 Gremlin 唯一資源識別符 (UFI)
<Azure-Cosmos-DB-account>.gremlin.cosmos.azure.com
AZURE_COSMOS_PORT
Gremlin 連線埠
10350
AZURE_COSMOS_USERNAME
您的用戶名稱
</dbs/<database>/colls/<collection or graphs>
AZURE_COSMOS_CLIENTID
您的用戶端識別碼
<client-ID>
AZURE_COSMOS_CLIENTSECRET
您的客戶端密碼
<client-secret>
AZURE_COSMOS_TENANTID
您的租用戶識別碼
<tenant-ID>
範例指令碼
請參閱下面的步驟和程序代碼,以使用服務主體連線到適用於 Gremlin 的 Azure Cosmos DB。
安裝相依性。
dotnet add package Gremlin.Net
dotnet add package Azure.Identity
使用客戶端連結庫 Azure.Identity 取得受控識別或服務主體的存取令牌。 使用存取令牌並 AZURE_COSMOS_LISTKEYURL
取得密碼。 從 Service 連線 or 所新增的環境變數取得連線資訊,並連線到適用於 Apache Gremlin 的 Azure Cosmos DB。 使用下列程式代碼時,請取消批註您想要使用的驗證類型代碼段部分。
using System;
using System.Security.Authentication;
using System.Net.Security;
using System.Net.Http;
using System.Security.Authentication;
using System.Threading.Tasks;
using System;
using Gremlin.Net.Driver;
using Azure.Identity;
var gremlinEndpoint = Environment.GetEnvironmentVariable("AZURE_COSMOS_RESOURCEENDPOINT");
var userName = Environment.GetEnvironmentVariable("AZURE_COSMOS_USERNAME");
var gremlinPort = Int32.Parse(Environment.GetEnvironmentVariable("AZURE_COSMOS_PORT"));
var listKeyUrl = Environment.GetEnvironmentVariable("AZURE_COSMOS_LISTKEYURL");
var scope = Environment.GetEnvironmentVariable("AZURE_COSMOS_SCOPE");
// Uncomment the following lines according to the authentication type.
// For system-assigned identity.
// var tokenProvider = new DefaultAzureCredential();
// For user-assigned identity.
// var tokenProvider = new DefaultAzureCredential(
// new DefaultAzureCredentialOptions
// {
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTID");
// }
// );
// For service principal.
// var tenantId = Environment.GetEnvironmentVariable("AZURE_COSMOS_TENANTID");
// var clientId = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTID");
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_COSMOS_CLIENTSECRET");
// var tokenProvider = new ClientSecretCredential(tenantId, clientId, clientSecret);
// Acquire the access token.
AccessToken accessToken = await tokenProvider.GetTokenAsync(
new TokenRequestContext(scopes: new string[]{ scope }));
// Get the password.
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {accessToken.Token}");
var response = await httpClient.POSTAsync(listKeyUrl);
var responseBody = await response.Content.ReadAsStringAsync();
var keys = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseBody);
var password = keys["primaryMasterKey"];
// Connect to Azure Cosmos DB for Apache Gremlin
var server = new GremlinServer(
hostname: gremlinEndpoint,
port: gremlinPort,
username: userName,
password: password,
enableSsl: true
);
using var client = new GremlinClient(
gremlinServer: server,
messageSerializer: new Gremlin.Net.Structure.IO.GraphSON.GraphSON2MessageSerializer()
);
在您的pom.xml檔案中 新增下列相依性:
<dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>gremlin-driver</artifactId>
<version>3.4.13</version>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.1.5</version>
</dependency>
使用 azure-identity
取得受控識別或服務主體的存取令牌。 使用存取令牌並 AZURE_COSMOS_LISTKEYURL
取得密碼。 從 Service 連線 or 所新增的環境變數取得連線資訊,並連線到適用於 Apache Gremlin 的 Azure Cosmos DB。 使用下列程式代碼時,請取消批註您想要使用的驗證類型代碼段部分。
import org.apache.tinkerpop.gremlin.driver.Client;
import org.apache.tinkerpop.gremlin.driver.Cluster;
import javax.net.ssl.*;
import java.net.InetSocketAddress;
import com.azure.identity.*;
import com.azure.core.credentital.*;
import java.net.http.*;
import java.net.URI;
int gremlinPort = Integer.parseInt(System.getenv("AZURE_COSMOS_PORT"));
String username = System.getenv("AZURE_COSMOS_USERNAME");
String gremlinEndpoint = System.getenv("AZURE_COSMOS_RESOURCEENDPOINT");
String listKeyUrl = System.getenv("AZURE_COSMOS_LISTKEYURL");
String scope = System.getenv("AZURE_COSMOS_SCOPE");
// Uncomment the following lines according to the authentication type.
// For system managed identity.
// DefaultAzureCredential defaultCredential = new DefaultAzureCredentialBuilder().build();
// For user assigned managed identity.
// DefaultAzureCredential defaultCredential = new DefaultAzureCredentialBuilder()
// .managedIdentityClientId(System.getenv("AZURE_COSMOS_CLIENTID"))
// .build();
// For service principal.
// ClientSecretCredential defaultCredential = new ClientSecretCredentialBuilder()
// .clientId(System.getenv("AZURE_COSMOS_CLIENTID"))
// .clientSecret(System.getenv("AZURE_COSMOS_CLIENTSECRET"))
// .tenantId(System.getenv("AZURE_COSMOS_TENANTID"))
// .build();
// Get the access token.
AccessToken accessToken = defaultCredential.getToken(new TokenRequestContext().addScopes(new String[]{ scope })).block();
String token = accessToken.getToken();
// Get the password.
HttpClient client = HttpClient.newBuilder().build();
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI(listKeyUrl))
.header("Authorization", "Bearer " + token)
.POST()
.build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
JSONParser parser = new JSONParser();
JSONObject responseBody = parser.parse(response.body());
String gremlinPassword = responseBody.get("primaryMasterKey");
// Connect to Azure Cosmos DB for Apache Gremlin
Cluster cluster;
Client client;
cluster = Cluster.addContactPoint(gremlinEndpoint).port(gremlinPort).credentials(username, password).create();
安裝相依性。
pip install gremlinpython
使用 azure-identity
來向受控識別或服務主體進行驗證,並傳送要求以 AZURE_COSMOS_LISTKEYURL
取得密碼。 從 Service 連線 or 所新增的環境變數取得連線資訊,並連線到適用於 Apache Gremlin 的 Azure Cosmos DB。 使用下列程式代碼時,請取消批註您想要使用的驗證類型代碼段部分。
from gremlin_python.driver import client, serializer
import requests
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
username = os.getenv('AZURE_COSMOS_USERNAME')
endpoint = os.getenv('AZURE_COSMOS_RESOURCEENDPOINT')
port = os.getenv('AZURE_COSMOS_PORT')
listKeyUrl = os.getenv('AZURE_COSMOS_LISTKEYURL')
scope = os.getenv('AZURE_COSMOS_SCOPE')
# Uncomment the following lines according to the authentication type.
# For system-assigned managed identity
# cred = ManagedIdentityCredential()
# For user-assigned managed identity
# managed_identity_client_id = os.getenv('AZURE_COSMOS_CLIENTID')
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
# For service principal
# tenant_id = os.getenv('AZURE_COSMOS_TENANTID')
# client_id = os.getenv('AZURE_COSMOS_CLIENTID')
# client_secret = os.getenv('AZURE_COSMOS_CLIENTSECRET')
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
# Get the password
session = requests.Session()
token = cred.get_token(scope)
response = session.post(listKeyUrl, headers={"Authorization": "Bearer {}".format(token.token)})
keys_dict = response.json()
password = keys_dict['primaryMasterKey']
# Connect to Azure Cosmos DB for Apache Gremlin.
client = client.Client(
url=endpoint,
traversal_source="g",
username=username,
password=password,
message_serializer=serializer.GraphSONSerializersV2d0(),
)
安裝相依性。
go get github.com/Azure/azure-sdk-for-go/sdk/azidentity
go get github.com/go-gremlin/gremlin
在程式代碼中,使用 azidentity
取得存取令牌,然後使用它來取得密碼。 從 Service 連線 or 所新增的環境變數取得連線資訊,並連線到適用於 Apache Gremlin 的 Azure Cosmos DB。 使用下列程式代碼時,請取消批註您想要使用的驗證類型代碼段部分。
import (
"fmt"
"os"
"context"
"log"
"io/ioutil"
"encoding/json"
"github.com/go-gremlin/gremlin"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
)
func main() {
username = os.Getenv("AZURE_COSMOS_USERNAME")
endpoint = os.getenv("AZURE_COSMOS_RESOURCEENDPOINT")
port = os.getenv("AZURE_COSMOS_PORT")
listKeyUrl = os.Getenv("AZURE_COSMOS_LISTKEYURL")
scope = os.Getenv("AZUE_COSMOS_SCOPE")
// Uncomment the following lines according to the authentication type.
// For system-assigned identity.
// cred, err := azidentity.NewDefaultAzureCredential(nil)
// For user-assigned identity.
// clientid := os.Getenv("AZURE_COSMOS_CLIENTID")
// azidentity.ManagedIdentityCredentialOptions.ID := clientid
// options := &azidentity.ManagedIdentityCredentialOptions{ID: clientid}
// cred, err := azidentity.NewManagedIdentityCredential(options)
// For service principal.
// clientid := os.Getenv("AZURE_COSMOS_CLIENTID")
// tenantid := os.Getenv("AZURE_COSMOS_TENANTID")
// clientsecret := os.Getenv("AZURE_COSMOS_CLIENTSECRET")
// cred, err := azidentity.NewClientSecretCredential(tenantid, clientid, clientsecret, &azidentity.ClientSecretCredentialOptions{})
// Acquire the access token.
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
token, err := cred.GetToken(ctx, policy.TokenRequestOptions{
Scopes: []string{scope},
})
// Acquire the connection string.
client := &http.Client{}
req, err := http.NewRequest("POST", listKeyUrl, nil)
req.Header.Add("Authorization", "Bearer " + token.Token)
resp, err := client.Do(req)
body, err := ioutil.ReadAll(resp.Body)
var result map[string]interface{}
json.Unmarshal(body, &result)
password, err := result["primaryMasterKey"];
auth := gremlin.OptAuthUserPass(username, password)
client, err := gremlin.NewClient(endpoint, auth)
}
安裝相依性。
npm install gremlin
npm install --save @azure/identity
在程式代碼中,使用 @azure/identity
取得存取令牌,然後使用它來取得密碼。 從 Service 連線 or 所新增的環境變數取得連線資訊,並連線到適用於 Apache Gremlin 的 Azure Cosmos DB。 使用下列程式代碼時,請取消批註您想要使用的驗證類型代碼段部分。
import { DefaultAzureCredential,ClientSecretCredential } from "@azure/identity";
import gremlin from 'gremlin'
const axios = require('axios');
let username = process.env.AZURE_COSMOS_USERNAME;
let endoint = process.env.AZURE_COSMOS_RESOURCEENDPOINT;
let port = process.env.AZURE_COSMOS_PORT;
let listKeyUrl = process.env.AZURE_COSMOS_LISTKEYURL;
let scope = process.env.AZURE_COSMOS_SCOPE;
// Uncomment the following lines according to the authentication type.
// For system-assigned identity.
// const credential = new DefaultAzureCredential();
// For user-assigned identity.
// const clientId = process.env.AZURE_COSMOS_CLIENTID;
// const credential = new DefaultAzureCredential({
// managedIdentityClientId: clientId
// });
// For service principal.
// const tenantId = process.env.AZURE_COSMOS_TENANTID;
// const clientId = process.env.AZURE_COSMOS_CLIENTID;
// const clientSecret = process.env.AZURE_COSMOS_CLIENTSECRET;
// Acquire the access token.
var accessToken = await credential.getToken(scope);
// Get the password.
const config = {
method: 'post',
url: listKeyUrl,
headers: {
'Authorization': `Bearer ${accessToken.token}`
}
};
const response = await axios(config);
const keysDict = response.data;
const password = keysDict['primaryMasterKey'];
const credentials = new gremlin.driver.auth.PlainTextSaslAuthenticator(
username,
password
)
const client = new gremlin.driver.Client(
endpoint,
{
credentials,
traversalsource: 'g',
rejectUnauthorized: true,
mimeType: 'application/vnd.gremlin-v2.0+json'
}
)
client.open()
在上呼叫 REST API AZURE_COSMOS_LISTKEYURL
,以取得具有受控識別或服務主體的存取令牌,以取得 Azure Cosmos DB for Gremlin 的主要密鑰。
$endpoint = getenv('AZURE_COSMOS_RESOURCEENDPOINT');
$username = getenv('AZURE_COSMOS_USERNAME');
$port = getenv('AZURE_COSMOS_PORT');
$db = new Connection([
'host' => $endpoint,
'username' => $username,
'password' => $password,
'port' => $port,
'ssl' => TRUE
]);
下一步
請遵循下列教學課程,深入瞭解 Service 連線 or。