Python3 and django3 Google Business API Error

209 Views Asked by At

I have the following code in python 3 and django 3. I have all previous google api steps done documented in the API, when I run my server I can finish the process and obtain a credential access_token, But when I try to use it it fails when I try to execute a request to business API methods.

from django.http import HttpResponseRedirect,

from googleapiclient.discovery import build
from google.oauth2.credentials import Credentials
import google_auth_oauthlib.flow
from django.views.decorators.clickjacking import xframe_options_exempt

from .models import GoogleCredentialsModel

CLIENT_SECRETS = os.path.join(os.path.dirname(__file__), 'client_secrets.json')
SCOPES = ['https://www.googleapis.com/auth/business.manage',]

@xframe_options_exempt
def auth_request(request):
    user = User.objects.get(id=request.user.id)  # request.user
    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
        CLIENT_SECRETS,
        SCOPES,
    )
    flow.redirect_uri = settings.GOOGLE_BUSINESS_CALLBACK
    authoritation_url, state = flow.authorization_url(
    #     # Enable offline access so that you can refresh an access token without
    #     # re-prompting the user for permission. Recommended for web server apps.
         access_type='offline',
    #     # Enable incremental authorization. Recommended as a best practice.
         include_granted_scopes='true',
    #     # ask always if consent
         prompt='consent'
    )
    return HttpResponseRedirect(authoritation_url)

@xframe_options_exempt
def auth_return(request):
    state = request.GET["state"]
    code = request.GET["code"]
    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
        CLIENT_SECRETS,
        SCOPES,
        state=state)
    flow.redirect_uri = settings.GOOGLE_BUSINESS_CALLBACK
    try:
        flow.fetch_token(code=code)
        user = request.user
        user = TSMUser.objects.get(id=user.id)
        if GoogleCredentialModel.objects.filter(user=user).exists():
            gcred = GoogleCredentialModel.objects.get(user=user)
            gcred.set_data(Credentials(**flow.credentials))
        else:
            GoogleCredentialModel.objects.create(user=user,
                            credential=Credentials(**flow.credentials))
        service = build('business', 'v4', discoveryServiceUrl='https://developers.google.com/my-business/samples/mybusiness_google_rest_v4p5.json', credentials=flow.credentials)
        list = service.accounts().list().execute()
        # Previous  line returns <HttpError 404 when requesting https://mybusiness.googleapis.com/v4/accounts?alt=json returned "Method not found.">

    except:
        pass
    return render('google_business.html', {'business_list': list.json()})

But I can't solve or find a solution for this error in python 3 Every site I search I find documentation of old version using python2, oauth2client and httplib2

In this link are samples using python 2 and oauth2client: https://github.com/googleapis/google-api-python-client

1

There are 1 best solutions below

0
On

The problem in my case was that I was adding a wrong client secret file, when I add the correct this code start working. I left it for if anyone needed