File tree Expand file tree Collapse file tree 4 files changed +40
-7
lines changed
Expand file tree Collapse file tree 4 files changed +40
-7
lines changed Original file line number Diff line number Diff line change 2121// [END apikeys_create_api_key]
2222// [START apikeys_authenticate_api_key]
2323#include " google/cloud/language/v1/language_client.h"
24- #include " google/cloud/common_options.h"
25- #include " google/cloud/grpc_options.h"
24+ #include " google/cloud/credentials.h"
2625#include " google/cloud/options.h"
2726
2827// [END apikeys_authenticate_api_key]
@@ -83,10 +82,8 @@ void AuthenticateWithApiKey(std::vector<std::string> const& argv) {
8382 " authenticate-with-api-key <project-id> <api-key>" };
8483 }
8584 namespace gc = ::google::cloud;
86- auto options =
87- gc::Options{}
88- .set <gc::GrpcCredentialOption>(grpc::SslCredentials ({}))
89- .set <gc::CustomHeadersOption>({{" x-goog-api-key" , argv[1 ]}});
85+ auto options = gc::Options{}.set <gc::UnifiedCredentialsOption>(
86+ gc::MakeApiKeyCredentials (argv[1 ]));
9087 auto client = gc::language_v1::LanguageServiceClient (
9188 gc::language_v1::MakeLanguageServiceConnection (options));
9289
Original file line number Diff line number Diff line change @@ -57,6 +57,12 @@ std::shared_ptr<Credentials> MakeExternalAccountCredentials(
5757 std::move (json_object), std::move (opts));
5858}
5959
60+ std::shared_ptr<Credentials> MakeApiKeyCredentials (std::string api_key,
61+ Options opts) {
62+ return std::make_shared<internal::ApiKeyConfig>(std::move (api_key),
63+ std::move (opts));
64+ }
65+
6066GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
6167} // namespace cloud
6268} // namespace google
Original file line number Diff line number Diff line change @@ -276,6 +276,27 @@ std::shared_ptr<Credentials> MakeServiceAccountCredentials(
276276std::shared_ptr<Credentials> MakeExternalAccountCredentials (
277277 std::string json_object, Options opts = {});
278278
279+ /* *
280+ * Create credentials that authenticate using an [API key].
281+ *
282+ * API keys are convenient because no [principal] is needed. The API key
283+ * associates the request with a Google Cloud project for billing and quota
284+ * purposes.
285+ *
286+ * @note Most Cloud APIs do not support API keys, instead requiring full
287+ * credentials.
288+ *
289+ * @note This authentication scheme does not involve access tokens. The returned
290+ * `Credentials` are incompatible with an `oauth2::AccessTokenGenerator`.
291+ *
292+ * @ingroup guac
293+ *
294+ * [API key]: https://cloud.google.com/docs/authentication/api-keys-use
295+ * [principal]: https://cloud.google.com/docs/authentication#principal
296+ */
297+ std::shared_ptr<Credentials> MakeApiKeyCredentials (std::string api_key,
298+ Options opts = {});
299+
279300/* *
280301 * Configure the delegates for `MakeImpersonateServiceAccountCredentials()`
281302 *
Original file line number Diff line number Diff line change @@ -30,7 +30,7 @@ using ::testing::IsNull;
3030TEST (Credentials, ErrorCredentials) {
3131 TestCredentialsVisitor visitor;
3232
33- auto credentials = internal:: MakeErrorCredentials ({});
33+ auto credentials = MakeErrorCredentials ({});
3434 CredentialsVisitor::dispatch (*credentials, visitor);
3535 EXPECT_EQ (" ErrorCredentialsConfig" , visitor.name );
3636}
@@ -113,6 +113,15 @@ TEST(Credentials, ExternalAccount) {
113113 ElementsAre (" scope1" , " scope2" ));
114114}
115115
116+ TEST (Credentials, ApiKeyCredentials) {
117+ TestCredentialsVisitor visitor;
118+
119+ auto credentials = MakeApiKeyCredentials (" api-key" );
120+ CredentialsVisitor::dispatch (*credentials, visitor);
121+ EXPECT_EQ (" ApiKeyConfig" , visitor.name );
122+ EXPECT_EQ (" api-key" , visitor.api_key );
123+ }
124+
116125} // namespace
117126} // namespace internal
118127GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
You can’t perform that action at this time.
0 commit comments