Stackdriver Monitoring API: authentication using API key does not work

398 views
Skip to first unread message

mart...@uber.com

unread,
May 11, 2018, 5:01:38 AM5/11/18
to Google Stackdriver Discussion Forum
I have created an API key (https://console.cloud.google.com/apis/credentials?project=my-project) and restricted its use to Stackdriver Monitoring API and Stackdriver API.

When I attempt to create an alert policy using a curl request, I get a 401 error.

curl -d "@create_some_alert_policy_request.json" -H 'Content-Type: application/json' https://monitoring.googleapis.com/v3/projects/my-project/alertPolicies?key=MY_KEY

{
  "error": {
    "code": 401,
    "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
    "status": "UNAUTHENTICATED"
  }
}

Michael Safyan

unread,
May 11, 2018, 11:51:02 AM5/11/18
to mart...@uber.com, Google Stackdriver Discussion Forum
Only some APIs allow for just an API key. An API key is suitable when there is no private data, and the API simply needs to attribute requests to a particular project for the purpose of tracking quota. In the case of this API, it operates on data that is access-controlled, and thus it needs an end user credential, which can be an ordinary user OAuth2 access token or an OAuth2 token generated from a service account (a robot account created for automation purposes).

The APIs that require an OAuth2 access token expect the token in a header:

                    Authorization: Bearer access_token

Or in a URL parameter:

                    ....?access_token=access_token

For scripting, you can use oauth2l to help you generate these tokens:

                     curl -H "$(oauth2l header cloud-platform)" ...

This will generate an appropriate "Authorization: Bearer ..." header assuming that you have active application-default credentials or active service account credentials (which you can provision locally using the Google Cloud SDKI command line; see gcloud auth application-default login).

For more details, see:


Michael Safyan

Senior Software Engineer · Stackdriver Monitoring

6425 Penn Ave 7th Floor; Pittsburgh, PA 15206

http://www.michaelsafyan.com | michae...@google.com



--
© 2016 Google Inc. 1600 Amphitheatre Parkway, Mountain View, CA 94043
 
Email preferences: You received this email because you signed up for the Google Stackdriver Discussion Google Group (google-stackdr...@googlegroups.com) to participate in discussions with other members of the GoogleStackdriver community.
---
You received this message because you are subscribed to the Google Groups "Google Stackdriver Discussion Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-stackdriver-d...@googlegroups.com.
To post to this group, send email to google-stackdr...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-stackdriver-discussion/7853adb2-a07f-4e32-bf01-3630f7b46dc4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages