示例#1
0
def authorize_access(g_contact, reauthorize=None):
    """
		If no Authorization code get it from Google and then request for Refresh Token.
		Google Contact Name is set to flags to set_value after Authorization Code is obtained.
	"""

    google_settings = frappe.get_doc("Google Settings")
    google_contact = frappe.get_doc("Google Contacts", g_contact)

    redirect_uri = get_request_site_address(
        True
    ) + "?cmd=frappe.integrations.doctype.google_contacts.google_contacts.google_callback"

    if not google_contact.authorization_code or reauthorize:
        frappe.cache().hset("google_contacts", "google_contact",
                            google_contact.name)
        return get_authentication_url(client_id=google_settings.client_id,
                                      redirect_uri=redirect_uri)
    else:
        try:
            data = {
                "code":
                google_contact.authorization_code,
                "client_id":
                google_settings.client_id,
                "client_secret":
                google_settings.get_password(fieldname="client_secret",
                                             raise_exception=False),
                "redirect_uri":
                redirect_uri,
                "grant_type":
                "authorization_code"
            }
            r = requests.post(get_auth_url(), data=data).json()

            if "refresh_token" in r:
                frappe.db.set_value("Google Contacts", google_contact.name,
                                    "refresh_token", r.get("refresh_token"))
                frappe.db.commit()

            frappe.local.response["type"] = "redirect"
            frappe.local.response[
                "location"] = "/app/Form/Google%20Contacts/{}".format(
                    google_contact.name)

            frappe.msgprint(_("Google Contacts has been configured."))
        except Exception as e:
            frappe.throw(e)
示例#2
0
def authorize_access(reauthorize=None):
    """If no Authorization code get it from Google and then request for Refresh Token."""

    google_settings = frappe.get_doc("Google Settings")
    website_settings = frappe.get_doc("Website Settings")

    redirect_uri = (
        get_request_site_address(True) +
        "?cmd=frappe.website.doctype.website_settings.google_indexing.google_callback"
    )

    if not website_settings.indexing_authorization_code or reauthorize:
        return get_authentication_url(client_id=google_settings.client_id,
                                      redirect_uri=redirect_uri)
    else:
        try:
            data = {
                "code":
                website_settings.indexing_authorization_code,
                "client_id":
                google_settings.client_id,
                "client_secret":
                google_settings.get_password(fieldname="client_secret",
                                             raise_exception=False),
                "redirect_uri":
                redirect_uri,
                "grant_type":
                "authorization_code",
            }
            res = requests.post(get_auth_url(), data=data).json()

            if "refresh_token" in res:
                frappe.db.set_value("Website Settings", website_settings.name,
                                    "indexing_refresh_token",
                                    res.get("refresh_token"))
                frappe.db.commit()

            frappe.local.response["type"] = "redirect"
            frappe.local.response["location"] = "/app/Form/{0}".format(
                quote("Website Settings"))

            frappe.msgprint(_("Google Indexing has been configured."))
        except Exception as e:
            frappe.throw(e)
示例#3
0
def get_google_drive_object():
	"""
		Returns an object of Google Drive.
	"""
	google_settings = frappe.get_doc("Google Settings")
	account = frappe.get_doc("Google Drive")

	credentials_dict = {
		"token": account.get_access_token(),
		"refresh_token": account.get_password(fieldname="refresh_token", raise_exception=False),
		"token_uri": get_auth_url(),
		"client_id": google_settings.client_id or frappe.conf.google_client_id,
		"client_secret": google_settings.get_password(fieldname="client_secret", raise_exception=False) or frappe.conf.google_client_secret,
		"scopes": "https://www.googleapis.com/auth/drive/v3"
	}

	credentials = google.oauth2.credentials.Credentials(**credentials_dict)
	google_drive = googleapiclient.discovery.build("drive", "v3", credentials=credentials)

	return google_drive, account
	def get_access_token(self):
		google_settings = self.validate()

		if not self.refresh_token:
			button_label = frappe.bold(_("Allow Google Calendar Access"))
			raise frappe.ValidationError(_("Click on {0} to generate Refresh Token.").format(button_label))

		data = {
			"client_id": google_settings.client_id,
			"client_secret": google_settings.get_password(fieldname="client_secret", raise_exception=False),
			"refresh_token": self.get_password(fieldname="refresh_token", raise_exception=False),
			"grant_type": "refresh_token",
			"scope": SCOPES
		}

		try:
			r = requests.post(get_auth_url(), data=data).json()
		except requests.exceptions.HTTPError:
			button_label = frappe.bold(_("Allow Google Calendar Access"))
			frappe.throw(_("Something went wrong during the token generation. Click on {0} to generate a new one.").format(button_label))

		return r.get("access_token")
示例#5
0
def get_google_contacts_object(g_contact):
	"""
	Returns an object of Google Calendar along with Google Calendar doc.
	"""
	google_settings = frappe.get_doc("Google Settings")
	account = frappe.get_doc("Google Contacts", g_contact)

	credentials_dict = {
		"token": account.get_access_token(),
		"refresh_token": account.get_password(fieldname="refresh_token", raise_exception=False),
		"token_uri": get_auth_url(),
		"client_id": google_settings.client_id,
		"client_secret": google_settings.get_password(fieldname="client_secret", raise_exception=False),
		"scopes": "https://www.googleapis.com/auth/contacts",
	}

	credentials = google.oauth2.credentials.Credentials(**credentials_dict)
	google_contacts = build(
		serviceName="people", version="v1", credentials=credentials, static_discovery=False
	)

	return google_contacts, account
示例#6
0
    def get_access_token(self):
        import requests

        google_settings = frappe.get_doc("Google Settings")

        if not google_settings.enable:
            frappe.throw(_("Google Integration is disabled."))

        if not self.indexing_refresh_token:
            button_label = frappe.bold(_("Allow API Indexing Access"))
            raise frappe.ValidationError(
                _("Click on {0} to generate Refresh Token.").format(
                    button_label))

        data = {
            "client_id":
            google_settings.client_id,
            "client_secret":
            google_settings.get_password(fieldname="client_secret",
                                         raise_exception=False),
            "refresh_token":
            self.get_password(fieldname="indexing_refresh_token",
                              raise_exception=False),
            "grant_type":
            "refresh_token",
            "scope":
            INDEXING_SCOPES
        }

        try:
            res = requests.post(get_auth_url(), data=data).json()
        except requests.exceptions.HTTPError:
            button_label = frappe.bold(_("Allow Google Indexing Access"))
            frappe.throw(
                _("Something went wrong during the token generation. Click on {0} to generate a new one."
                  ).format(button_label))

        return res.get("access_token")
def get_google_calendar_object(g_calendar):
	"""
		Returns an object of Google Calendar along with Google Calendar doc.
	"""
	google_settings = frappe.get_doc("Google Settings")
	account = frappe.get_doc("Google Calendar", g_calendar)

	credentials_dict = {
		"token": account.get_access_token(),
		"refresh_token": account.get_password(fieldname="refresh_token", raise_exception=False),
		"token_uri": get_auth_url(),
		"client_id": google_settings.client_id,
		"client_secret": google_settings.get_password(fieldname="client_secret", raise_exception=False),
		"scopes": "https://www.googleapis.com/auth/calendar/v3"
	}

	credentials = google.oauth2.credentials.Credentials(**credentials_dict)
	google_calendar = googleapiclient.discovery.build("calendar", "v3", credentials=credentials)

	check_google_calendar(account, google_calendar)

	account.load_from_db()
	return google_calendar, account