Hey everyone, I need some help programmatically ac...
# troubleshoot
g
Hey everyone, I need some help programmatically accessing the graphql API endpoint in Python. I generated a PAT using this code:
Copy code
# query = """mutation {
#   createAccessToken(input: {type: PERSONAL, actorUrn: "urn:li:corpuser:datahub", duration: NO_EXPIRY, name: "my personal token"}) {
#     accessToken
#     metadata {
#       id
#       name
#       description
#     }
#   }
# }
# """
Then I went ahead and added it to my recipe:
Copy code
source:
  type: postgres
  config:
    # Coordinates
    host_port: localhost:5432
    database: andreasmartinson

    # Credentials
    username: ${USERNAME}
    password: ${PASSWORD}

    # Options
    database_alias: postgres

# <https://datahubproject.io/docs/authentication/introducing-metadata-service-authentication/>
sink:
  type: "datahub-rest"
  config:
    server: "<http://localhost:8080>"
    token: ${DATAHUB_TOKEN}
I then re-ingested the metadata and passed in the token:
Copy code
datahub ingest -c config/postgres_datahub_config.dhub.yaml
Then I tried this query:
Copy code
import requests

query = """query search {
  search(input: {type: DATASET, query: "*", start: 0, count: 100}) {
    searchResults {
      entity {
        ... on Dataset {
          properties {
            name
            description
          }
          schemaMetadata {
            name
            fields {
              fieldPath
              nativeDataType
            }
          }
        }
      }
    }
  }
}
"""
url = '<http://localhost:9002/api/graphiql>'
r = <http://requests.post|requests.post>(url, json={'query': query})
print(r.status_code)
print(r.text)
I verified that the query worked using the graphql frontend, it's just that I keep getting a 401 permissions error. Could someone help me figure out why I might be getting a permissions error? Feel like I'm probably missing something simple here
✅ 1
h
Hey @gentle-lifeguard-88494 assuming that you have enabled metadata service authentication for using personal access tokens, you'll not be able to use datahub gms url in server. You'll need to use DataHub Frontend Proxy here.
server: <http://localhost:9002/api/gms>
g
@hundreds-photographer-13496 Thanks for your help! I was actually able to get it working. Couple things I learned - sharing for others: • I had misspelled 'graphql' in my url string (graphiql instead of graphql) • I enabled authentication by updating the docker compose file for the gms and front end pods: https://github.com/datahub-project/datahub/issues/5159#issuecomment-1156248351 (thanks @incalculable-ocean-74010) • I used postman to get it working first and then went ahead and exported the Python to see what I was getting wrong

https://www.youtube.com/watch?v=DPY0G3Ix7Y8&amp;t=288s▾

(thanks @big-carpet-38439 ) • I could use both 8080 (backend) and 9002 (frontend) ports to query Here is what ended up working to get the first 100 tables in datahub using a GraphQL API call:
Copy code
import requests
import os
import json

datahub_key = os.getenv("DATAHUB_TOKEN")

query = json.dumps({'query':"""
query search {
  search(input: {type: DATASET, query: "*", start: 0, count: 100}) {
    searchResults {
      entity {
        ... on Dataset {
          properties {
            name
            description
          }
          schemaMetadata {
            name
            fields {
              fieldPath
              nativeDataType
            }
          }
        }
      }
    }
  }
}
"""})

headers = {
  'Authorization': f'Bearer {datahub_key}',
  'Content-Type': 'application/json'
}


url = '<http://localhost:9002/api/graphql>'
r = <http://requests.post|requests.post>(url, headers=headers, data=query)
print(r.status_code)
print(r.text)