gosom
gosom

Reputation: 1319

Provisioning APi redirect_uri

I am using https://developers.google.com/analytics/devguides/config/provisioning/v3/devguide

For authentication I use the "installed application method" and I obtained the following json from the developer console.

{
    "installed":{
            "auth_uri":"https://accounts.google.com/o/oauth2/auth",
            "client_secret":"MYSECRET",
            "token_uri":"https://accounts.google.com/o/oauth2/token",
            "client_email":"",
            "redirect_uris":["urn:ietf:wg:oauth:2.0:oob","oob"],
            "client_x509_cert_url":"",
            "client_id":"MY CLIENT ID",
            "auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs"
        } }

According to the documentation I have to do something like:

import httplib2
from apiclient.discovery import build
from oauth2client.file import Storage

def create_account_ticket(service):
    """for the body see
    https://developers.google.com/analytics/devguides/config/provisioning/v3/devguide
    search for  Create an Account Ticket using the Provisioning API
    and take a look at:
    https://developers.google.com/resources/api
    libraries/documentation/analytics/v3/python/latest/analytics_v3.provisioning.html
    """
  body_ = {'redirectUri': 'http://localhost',
          'account': {'name': "My Account Name"},
          'webproperty': {'name': 'What kind of name?',
                          'websiteUrl': 'http://www.mywebsite.de'},
          'profile': {'name': 'My Profile name', 'timezone': "Europe/Berlin"},
          }

  res = service.provisioning().createAccountTicket(body=body_).execute()
  return res


if __name__ == '__main__':
    storage = Storage('FileContainingToken.dat')
    credentials = storage.get()
    http = httplib2.Http()
    http = credentials.authorize(http)
    service = build('analytics', 'v3', http=http)
    t = create_account_ticket(service)

When I set the redirect_uri to localhost the response is:

HttpError: https://www.googleapis.com/analytics/v3/provisioning/createAccountTicket?alt=json returned "Value for field redirectUrl = `http://localhost` is not valid.">

When I remove the redirect_uri I get as expected: "Field redirect URI is required"

According to https://developers.google.com/accounts/docs/OAuth2InstalledApp#choosingredirecturi

localhost

This value signals to the Google Authorization Server that the authorization code should be returned as a query string parameter to the web server on the client

So this redirect_uri should be valid.

According to the provision api docs

Redirect URI - This is where the user is redirected to and the OAuth 2.0 response is sent. Configure Redirect URIs and obtain the Client ID for your project using the Google Developers Console. The value of this parameter must exactly match one of the values registered in the Google Developers Console (including the http or https schemes, case, and trailing '/').

How can I specify the redirect URI for the provisioning API and why the localhost is invalid?

Upvotes: 1

Views: 791

Answers (1)

SirLoyn
SirLoyn

Reputation: 121

Since I can't leave a comment yet, I'll leave it as this answer.

body_ = {'redirectUri': 'http//localhost',

Just at a glance,

http//localhost should be http://localhost

Upvotes: 1

Related Questions