Skip to content

Latest commit

 

History

History
170 lines (117 loc) · 7.88 KB

authenticate.md

File metadata and controls

170 lines (117 loc) · 7.88 KB

Authenticate to the HERE platform

To authenticate to the HERE platform and start working with HERE Data SDK for C++, you need to get an access token. You can receive it using a default token provider, project authentication, or federated credentials.

For instructions, see the OAuth tokens section in the Identity & Access Management Developer Guide.

Note

Keep your credentials secure and do not disclose them. Make sure that your credentials are not stored in a way that enables others to access them.

Authenticate using a default token provider

  1. Get your platform credentials.

    For instructions, see the Register your application section in the Identity & Access Management Developer Guide.

    You get the credentials.properties file.

  2. Initialize the authentication settings using the here.access.key.іd and here.access.key.secret from the credentials.properties file as kKeyId and kKeySecret respectively.

    Note

    You can also retrieve your credentials from the credentials.properties file using the ReadFromFile method. For more information, see the related API documentation.

    olp::authentication::Settings settings({kKeyId, kKeySecret});
    settings.task_scheduler = task_scheduler;
    settings.network_request_handler = http_client;
  3. Set up the AuthenticationSettings object with a default token provider.

    olp::client::AuthenticationSettings auth_settings;
    auth_settings.token_provider =
      olp::authentication::TokenProviderDefault(std::move(settings));

You get an access token.

You can use the AuthenticationSettings object to create the OlpClientSettings object. For more information, see the related section in the Developer Guide.

Authenticate using project authentication

  1. Get your platform credentials.

    For instructions, see the Register your application section in the Identity & Access Management Developer Guide.

  2. Initialize the AuthenticationCredentials class using the here.access.key.іd and here.access.key.secret from the credentials.properties file as kKeyId and kKeySecret respectively.

    Note

    You can also retrieve your credentials from the credentials.properties file using the ReadFromFile method. For more information, see the related API documentation.

    olp::authentication::AuthenticationCredentials credentials(kKeyId, kKeySecret);
  3. Create an authentication client.

    olp::authentication::AuthenticationSettings auth_client_settings;
    auth_client_settings.task_scheduler = task_scheduler;
    auth_client_settings.network_request_handler = http_client;
    olp::authentication::AuthenticationClient client(auth_client_settings);
  4. Create the SignInProperties object with your project ID.

    olp::authentication::AuthenticationClient::SignInProperties signin_properties;
    signin_properties.scope = "<project ID>";
  5. Call the SignInClient API on the previously created client object.

    client.SignInClient(
        credentials, signin_properties,
        [](olp::authentication::Response<olp::authentication::SignInResult>
               response) {
          // Handle the response
        });

You get an access token.

You can use the AuthenticationSettings object to create the OlpClientSettings object. For more information, see the related section in the Developer Guide.

Authenticate using federated credentials

  1. Get your platform credentials.

    For instructions, see the Register your application section in the Identity & Access Management Developer Guide.

    You get the credentials.properties file.

  2. Initialize the AuthenticationCredentials class using the here.access.key.іd and here.access.key.secret from the credentials.properties file as kKeyId and kKeySecret respectively.

    Note

    You can also retrieve your credentials from the credentials.properties file using the ReadFromFile method. For more information, see the related API documentation.

    olp::authentication::AuthenticationCredentials credentials(kKeyId, kKeySecret);
  3. Create an authentication client's settings.

    olp::authentication::AuthenticationSettings auth_client_settings;
    auth_client_settings.task_scheduler = task_scheduler;
    auth_client_settings.network_request_handler = http_client;
  4. Get your federated (Facebook or ArcGIS) properties.

    You should have at least your federated access token. For the complete list of federated properties, see the related documentation.

  5. Initialize your federated properties.

    olp::authentication::AuthenticationClient::FederatedProperties properties;
    properties.access_token = "your-access-token";
  6. Create your own token provider using the authentication client's settings created in step 3, your federated credentials.

    Note

    You can call your custom token provider form different threads.

    auto token = std::make_shared<olp::client::OauthToken>();
    
    olp::client::AuthenticationSettings auth_settings;
    auth_settings.token_provider =
        [token, auth_client_settings, credentials,
         properties](olp::client::CancellationContext context)
        -> olp::client::OauthTokenResponse {
      if (context.IsCancelled()) {
        return olp::client::ApiError::Cancelled();
      }
    
      if (!token->GetAccessToken().empty() &&
          std::chrono::system_clock::to_time_t(
              std::chrono::system_clock::now()) >= token->GetExpiryTime()) {
        return *token;
      }
    
      std::promise<olp::authentication::AuthenticationClient::SignInUserResponse>
          token_promise;
    
      auto callback =
          [&token_promise](
              olp::authentication::AuthenticationClient::SignInUserResponse
                  response) { token_promise.set_value(std::move(response)); };
    
      olp::authentication::AuthenticationClient client(auth_client_settings);
      client.SignInFacebook(credentials, properties, callback);
      auto response = token_promise.get_future().get();
      if (!response) {
        return response.GetError();
      }
    
      (*token) = olp::client::OauthToken(response.GetResult().GetAccessToken(),
                                         response.GetResult().GetExpiresIn());
    
      return *token;
    };

You get an access token. By default, it expires in 24 hours. To continue working with the HERE platform after your token expires, generate a new access token.

You can use the AuthenticationSettings object to create the OlpClientSettings object. For more information, see the related section in the Developer Guide.