Thomas K.
Thomas K.

Reputation: 11

Where do I put my api key in Google Cloud PHP Language Client?

I am trying to perform a sentiment analysis with the Google Cloud PHP language client from this tutorial: https://cloud.google.com/natural-language/docs/reference/libraries

In the documentation they say this should work with a plain api key: https://cloud.google.com/docs/authentication/api-keys

I already tried a couple of ways on how to set the api key (plain api key, no oauth), but I am always getting the error: "The request is missing a valid API key."

Here some of my tries:

// Instantiates a client
    $language = new LanguageClient([
        'projectId' => $projectId,
        'key' => $key,
        'developerKey' => $key,
        'api_key' => $key
    ]);
    $language->setDeveloperKey($key);

    // Detects the sentiment of the text
    $annotation = $language->analyzeSentiment($texttoanalyze);
    $sentiment = $annotation->sentiment();

    echo 'Text: ' . $text . 'Sentiment: ' . $sentiment['score'] . ', ' . $sentiment['magnitude'];

Upvotes: 0

Views: 1215

Answers (1)

Thomas K.
Thomas K.

Reputation: 11

ok, I figured out how to perform a plain api call without using the client library like this: POST https://language.googleapis.com/v1/documents:analyzeEntities?key=API_KEY

like described here: https://cloud.google.com/natural-language/docs/reference/rest/v1/documents/analyzeEntities

I could resolve the issue with the authentication for the client library by adding the parameter keyFilePath to the config of the LanguageClient like this:

$language = new LanguageClient([
        'projectId' => 'my-project-id',
        'keyFilePath' => '/path/to/my/keyfile.json'
    ]);

Upvotes: 1

Related Questions