コード例 #1
0
def _use_client_credentials(cognite_client: CogniteClient, client_credentials: Optional[Dict] = None) -> Optional[str]:
    """
    If client_credentials is passed, will use those, otherwise will implicitly use those the client was instantiated
    with
    Args:
        client_credentials: a dictionary containing:
            client_id
            client_secret

    Returns:
        nonce (optional, str): a nonce if able to obtain, otherwise returns None

    """

    if client_credentials:
        client_id = client_credentials["client_id"]
        client_secret = client_credentials["client_secret"]
    else:
        client_id = cognite_client.config.token_client_id
        client_secret = cognite_client.config.token_client_secret

    session_url = f"/api/playground/projects/{cognite_client.config.project}/sessions"
    payload = {"items": [{"clientId": f"{client_id}", "clientSecret": f"{client_secret}"}]}
    try:
        res = cognite_client.post(session_url, json=payload)
        nonce = res.json()["items"][0]["nonce"]
        return nonce
    except CogniteAPIError as e:
        print("Unable to get nonce using client credentials flow. The session API returned with error:", e.message)
        return None
コード例 #2
0
def _use_token_exchange(cognite_client: CogniteClient):
    session_url = f"/api/playground/projects/{cognite_client.config.project}/sessions"
    payload = {"items": [{"tokenExchange": True}]}
    try:
        res = cognite_client.post(url=session_url, json=payload)
        nonce = res.json()["items"][0]["nonce"]
        return nonce
    except CogniteAPIError as e:
        print("Unable to get nonce using token exchange flow. The session API returned with error:", e.message)