Django ZoHo Python SDK v2 CRM问题初始化

时间:2019-01-16 11:28:37

标签: django sdk crm zoho

我正在尝试将ZoHo crm v2 sdk与我的Django应用集成。 在Django运行服务器上,我无法获取访问令牌并使用refresh方法并将其存储在zcrm_oauthtokens.pkl文件中。然后,sdk使用刷新令牌自动刷新访问令牌,因此这里没有问题。但是在我的生产服务器(heroku)上,我收到此错误消息:

2019-01-16T11:07:22.314759+00:00 app[web.1]: 2019-01-16 11:07:22,314    - Client_Library_OAUTH - ERROR - Exception occured while fetching oauthtoken from db; Exception Message::'NoneType' object has no attribute 'accessToken'

在我看来,令牌已保存到文件中,但是当sdk尝试访问它们时,它是在数据库中而不是token_persistence_path中指定的文件中查找它们。

在我的settings.py中,我有这个:

ZOHO_CLIENT_ID = config('ZOHO_CLIENT_ID')
ZOHO_CLIENT_SECRET = config('ZOHO_CLIENT_SECRET')
ZOHO_REDIRECT_URI = config('ZOHO_REDIRECT_URI')
ZOHO_CURRENT_USER_EMAIL = 'jamesalexander@mylastwill.co.uk'
ZOHO_PATH = os.path.join(BASE_DIR, 'wills_online', 'zoho')

zoho_config = {'apiBaseUrl': "https://www.zohoapis.com",
               'currentUserEmail': ZOHO_CURRENT_USER_EMAIL,
               'client_id': ZOHO_CLIENT_ID,
               'client_secret': ZOHO_CLIENT_SECRET,
               'redirect_uri': ZOHO_REDIRECT_URI,
               'token_persistence_path': ZOHO_PATH}

在视图文件中,我有这个:

from zcrmsdk import *
import logging
from django.shortcuts import HttpResponse
from wills.models import PersonalDetails, ZoHoRecord, WillDocument
from wills_online.decorators import start_new_thread
from wills_online.settings import zoho_config
logger = logging.getLogger(__name__)


class ZohoRunOnce:
    def __init__(self):
        self.already_run = False

    def run_once(self):
        if not self.already_run:
            print('zoho init run once')
            ZCRMRestClient.initialize(zoho_config)
            self.already_run = True

zoho_init = ZohoRunOnce()
zoho_init.run_once()
print(zoho_config['token_persistence_path'])


def zoho_callback():
    return HttpResponse(200)

@start_new_thread
def zoho_personal_details(request):
    """ updates or create a user account on zoho on profile completion """

    personal_details_ob = PersonalDetails.objects.get(user=request.user)
    zoho_ob = ZoHoRecord.objects.get(user=request.user)

    try:
        if zoho_ob.account:
            record = ZCRMRecord.get_instance('Accounts', zoho_ob.account)
            record.set_field_value('Account_Name', request.user.email)
            record.set_field_value('Name', personal_details_ob.full_name)
            record.set_field_value('Email', request.user.email)
            record.set_field_value('Address_Line_1', personal_details_ob.address_line_1)
            record.set_field_value('Address_Line_2', personal_details_ob.address_line_2)
            record.set_field_value('Post_Town', personal_details_ob.post_town)
            record.set_field_value('Post_Code', personal_details_ob.post_code)
            record.set_field_value('Dob_Day', personal_details_ob.dob_day)
            record.set_field_value('Dob_Month', personal_details_ob.dob_month)
            record.set_field_value('Dob_Year', personal_details_ob.dob_year)
            record.set_field_value('Gender', personal_details_ob.sex)
            record.set_field_value('Marital_Status', personal_details_ob.marital_status)
            record.set_field_value('Partner_Name', personal_details_ob.partner_full_name)
            record.set_field_value('Partner_Gender', personal_details_ob.partner_gender)
            record.set_field_value('Partner_Email', personal_details_ob.partner_email)
            record.set_field_value('Children', personal_details_ob.children)
            record.set_field_value('Pets', personal_details_ob.pets)
            record.update()
        else:
            user = ZCRMUser.get_instance(name='James Alexander')
            record = ZCRMRecord.get_instance('Accounts')
            record.set_field_value('Account_Owner', user)
            record.set_field_value('Account_Name', request.user.email)
            record.set_field_value('Name', personal_details_ob.full_name)
            record.set_field_value('Email', request.user.email)
            record.set_field_value('Address_Line_1', personal_details_ob.address_line_1)
            record.set_field_value('Address_Line_2', personal_details_ob.address_line_2)
            record.set_field_value('Post_Town', personal_details_ob.post_town)
            record.set_field_value('Post_Code', personal_details_ob.post_code)
            record.set_field_value('Dob_Day', personal_details_ob.dob_day)
            record.set_field_value('Dob_Month', personal_details_ob.dob_month)
            record.set_field_value('Dob_Year', personal_details_ob.dob_year)
            record.set_field_value('Gender', personal_details_ob.sex)
            record.set_field_value('Marital_Status', personal_details_ob.marital_status)
            record.set_field_value('Partner_Name', personal_details_ob.partner_full_name)
            record.set_field_value('Partner_Gender', personal_details_ob.partner_gender)
            record.set_field_value('Partner_Email', personal_details_ob.partner_email)
            record.set_field_value('Children', personal_details_ob.children)
            record.set_field_value('Pets', personal_details_ob.pets)
            response = record.create()
            # save account id to db for future updates
            zoho_ob.account = response.details['id']
            zoho_ob.save()

    except ZCRMException as ex:
        logger.log(1, ex.status_code)
        logger.log(1, ex.error_message)
        logger.log(1, ex.error_details)
        logger.log(1, ex.error_content)
        print(ex.status_code)
        print(ex.error_message)
        print(ex.error_content)
        print(ex.error_details)

我尝试在settings.py中运行ZCRMRestClient.initialize(zoho_config),但没有成功。

我获得访问令牌和刷新令牌的方法似乎可行:

import os
import pprint
from sys import argv

import django
import requests
import zcrmsdk
from django.conf import settings

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'wills_online.settings')
django.setup()


def zoho_refresh_token(code):
    """ supply a self client token from the zoho api credentials from web site """

    zoho_config = {"apiBaseUrl": "https://www.zohoapis.com",
               "currentUserEmail": settings.ZOHO_CURRENT_USER_EMAIL,
               "client_id": settings.ZOHO_CLIENT_ID,
               "client_secret": settings.ZOHO_CLIENT_SECRET,
               "redirect_uri": settings.ZOHO_REDIRECT_URI,
               "token_persistence_path": settings.ZOHO_PATH}

pprint.pprint(zoho_config)

print('working')
address = f'https://accounts.zoho.com/oauth/v2/token?code={code}&redirect_uri={settings.ZOHO_REDIRECT_URI}&client_id={settings.ZOHO_CLIENT_ID}&client_secret={settings.ZOHO_CLIENT_SECRET}&grant_type=authorization_code'
response = requests.post(address)
data = response.json()
pprint.pprint(data)
zcrmsdk.ZCRMRestClient.initialize(zoho_config)
oauth_client = zcrmsdk.ZohoOAuth.get_client_instance()
refresh_token = data['refresh_token']
print(type(refresh_token))
oauth_client.generate_access_token_from_refresh_token(refresh_token, settings.ZOHO_CURRENT_USER_EMAIL)

print(refresh_token)
print('finished')

如果名称 =='主要”:     zoho_refresh_token(argv [1])

这让我发疯了。帮助将不胜感激。这是我的第一篇文章,轻松点,大声笑。

1 个答案:

答案 0 :(得分:1)

为将来参考,您将需要在配置字典中定义persistence_handler_classpersistence_handler_path。您还将需要一个处理程序类和一个用户定义的模型来存储结果。示例代码如下:

# settings.py

import zcrmsdk
configuration_dictionary = {
    'apiBaseUrl': 'https://www.zohoapis.com',
    'apiVersion': 'v2',
    'currentUserEmail': ZOHO_CURRENT_USER_EMAIL,
    'sandbox': 'False',
    'applicationLogFilePath': '',
    'client_id': ZOHO_CLIENT_ID,
    'client_secret': ZOHO_CLIENT_SECRET,
    'redirect_uri': ZOHO_REDIRECT_URI,
    'accounts_url': 'https://accounts.zoho.com',
    'access_type': 'online',
    'persistence_handler_class': ZOHO_HANDLER_CLASS,
    'persistence_handler_path': ZOHO_HANDLER_PATH,
}
zcrmsdk.ZCRMRestClient.initialize(configuration_dictionary)

# zoho.models.py

from django.db import models
from zcrmsdk.OAuthClient import ZohoOAuthTokens


class ZohoOAuthHandler:
    @staticmethod
    def get_oauthtokens(email_address):
        oauth_model_instance = ZohoOAuth.objects.get(user_email=email_address)
        return ZohoOAuthTokens(oauth_model_instance.refresh_token,
                               oauth_model_instance.access_token,
                               oauth_model_instance.expiry_time,
                               user_email=oauth_model_instance.user_email)

    @staticmethod
    def save_oauthtokens(oauth_token):
        defaults = {
            'refresh_token': oauth_token.refreshToken,
            'access_token': oauth_token.accessToken,
            'expiry_time': oauth_token.expiryTime,
        }
        ZohoOAuth.objects.update_or_create(user_email=oauth_token.userEmail, defaults=defaults)


class ZohoOAuth(models.Model):

    refresh_token = models.CharField(max_length=250)
    access_token = models.CharField(max_length=250)
    expiry_time = models.BigIntegerField()
    user_email = models.EmailField()

在此示例中,ZOHO_HANDLER_CLASS = 'ZohoOAuthHandler'ZOHO_HANDLER_PATH = 'zoho.models'

第一次使用时,您将需要https://accounts.zoho.com/developerconsole中的grant_token。对于范围,请使用aaaserver.profile.READ,ZohoCRM.modules.ALL开始(请参阅https://www.zoho.com/crm/developer/docs/api/oauth-overview.html#scopes

在使用api之前,您需要在django shell中运行以下代码。这使用授权令牌来生成您的初始访问和刷新令牌。之后,该api应该处理刷新您的访问令牌。

grant_token = GRANT_TOKEN
import zcrmsdk
oauth_client = zcrmsdk.ZohoOAuth.get_client_instance()
oauth_tokens = oauth_client.generate_access_token(grant_token)