Search code examples
curlgoogle-apigoogle-oauthgoogle-search-console

Google Search Console API with CURL OAuth


I want to use CURL to get monthly data for our website pages using Google Search Console API - keywords used, page impressions, etc. I have a Google account with domain verified. I have a project that has access to the API, and an API key.

I have searched and tested for hours.

Google documentation says I can use an API key and append it to the end of the URL:

https://www.googleapis.com/webmasters/v3/sites/https%3A%2F%2Fwww.example.com%2F/searchAnalytics/query?key={my API key}

The JSON I post to the API is:

{
"startDate": "2021-04-01",
"endDate\": \"2021-04-30",
"dimensions": ["PAGE","QUERY"]
}

I always get an authentication error: "Request is missing required authentication credential".

I have tried to understand OAuth. I just cannot understand it, nor explain exactly what I've done via various "help" articles. I cannot find out IF I need and HOW I get an authorisation token mentioned in tutorials in order to then request data. I'm lost and confused.

Can anyone explain how to use CURL to authenticate a request to Google Search Console API, using OAuth or an API key.


Solution

  • Api keys are only used for accessing public data the data you are looking to access is private data.

    To access private data you will need to use oauth2 to get an access token

    # Tutorial https://www.daimto.com/how-to-get-a-google-access-token-with-curl/
    # YouTube video https://youtu.be/hBC_tVJIx5w
    # Client id from Google Developer console
    # Client Secret from Google Developer console
    # Scope this is a space seprated list of the scopes of access you are requesting.
    
    # Authorization link.  Place this in a browser and copy the code that is returned after you accept the scopes.
    https://accounts.google.com/o/oauth2/auth?client_id=[Application Client Id]&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=[Scopes]&response_type=code
    
    # Exchange Authorization code for an access token and a refresh token.
    
    curl \
    --request POST \
    --data "code=[Authentcation code from authorization link]&client_id=[Application Client Id]&client_secret=[Application Client Secret]&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code" \
    https://accounts.google.com/o/oauth2/token
    
    # Exchange a refresh token for a new access token.
    curl \
    --request POST \
    --data 'client_id=[Application Client Id]&client_secret=[Application Client Secret]&refresh_token=[Refresh token granted by second step]&grant_type=refresh_token' \
    https://accounts.google.com/o/oauth2/token
    

    This video will walk you though it Understanding Google OAuth 2.0 with curl