Skip to content

Instantly share code, notes, and snippets.

@hsmalley
Created April 27, 2023 20:33
Show Gist options
  • Save hsmalley/cd648c3eed75f38680bed901f45a1cbf to your computer and use it in GitHub Desktop.
Save hsmalley/cd648c3eed75f38680bed901f45a1cbf to your computer and use it in GitHub Desktop.
Get the token for local logins
"""Module to read production and consumption values from an Enphase Envoy on the local network."""
"""https://support.enphase.com/s/question/0D53m00007a2WcDCAU/instructions-to-get-access-token"""
import asyncio
import logging
import re
import time
import jwt
from html.parser import HTMLParser
from json.decoder import JSONDecodeError
try:
from BeautifulSoup import BeautifulSoup
except ImportError:
from bs4 import BeautifulSoup
#from envoy_utils.envoy_utils import EnvoyUtils
import httpx
class MyHTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs):
print("Encountered a start tag:", tag)
def handle_endtag(self, tag):
print("Encountered an end tag :", tag)
def handle_data(self, data):
print("Encountered some data :", data)
SITE_ID = "<SiteID from online portal>"
SERIAL_NUMBER = "<EnvoySN from online portal>"
ENDPOINT_URL_PRODUCTION_JSON = "http://{}/production.json"
ENDPOINT_URL_PRODUCTION_V1 = "http://{}/api/v1/production"
ENDPOINT_URL_PRODUCTION_INVERTERS = "http://{}/api/v1/production/inverters"
ENDPOINT_URL_PRODUCTION = "http://{}/production"
LOGIN_URL = "https://entrez.enphaseenergy.com/login?utm_source=community&utm_medium=answer_links&utm_campaign=internal_reference"
TOKEN_URL = "https://entrez.enphaseenergy.com/entrez_tokens?utm_source=community&utm_medium=answer_links&utm_campaign=internal_reference"
LOCAL_URL = "https://<enphaseAddress>/"
payload_login = {'username': '<OnlinePortal-EmailAddress>', 'password': '<OnlinePortalPassword>'}
payload_token = {'Site': SITE_ID, "serialNum": SERIAL_NUMBER}
headers = {'Content-Type': 'application/json'}
client = httpx.Client(verify=False)
token = ""
try:
#r = client.get(LOCAL_URL + "production.json?details=1", timeout=30, auth=httpx.DigestAuth("envoy", "060808"))
#print(r.text)
r = client.post(LOGIN_URL, data=payload_login)
print(r.status_code)
#print(r.text)
r = client.post(TOKEN_URL, data=payload_token)
print(r.status_code)
#print(r.text)
parsed_html = BeautifulSoup(r.text, "lxml")
token = parsed_html.body.find('textarea').text
print(token)
decode = jwt.decode(token, options={"verify_signature": False}, algorithms="ES256")
print(decode["exp"])
header = {"Authorization": "Bearer " + token}
r = client.get(LOCAL_URL + "auth/check_jwt", headers=header)
#print(r.text)
r = client.get(LOCAL_URL + "production.json?details=1", headers=header)
#print(r.text)
finally:
client.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment