Python GraphQL gql 客户端身份验证

Mac*_*ias 8 python oauth graphql graphql-python

I\xc2\xb4m 很难将 GraphQL 与 Python 结合使用,因为建议的库:gql完全没有文档记录。

\n\n

我怎么发现要提供 api url,我需要将 RequestsHTTPTransport 对象传递给客户端,如下所示:

\n\n
client = Client(transport=RequestsHTTPTransport(url=\'https://some.api.com/v3/graphql\'))\n
Run Code Online (Sandbox Code Playgroud)\n\n

但如何提供像承载密钥这样的凭证呢?

\n\n

PS\n我注意到它 RequestsHTTPTransport 还接受一个身份验证参数,其描述为:

\n\n
:param auth: Auth tuple or callable to enable Basic/Digest/Custom HTTP Auth\n
Run Code Online (Sandbox Code Playgroud)\n\n

然而我仍然不知道如何创建这个元组或可调用来使用承载密钥:(

\n\n

谢谢指教

\n

小智 7

您可以将其添加到标题中。

reqHeaders = {
    'x-api-key' : API_KEY,
    'Authorization': 'Bearer ' + TOKEN_KEY // This is the key
}

_transport = RequestsHTTPTransport(
    url=API_ENDPOINT,
    headers = reqHeaders,
    use_json=True,
)

client = Client(
    transport = _transport,
    fetch_schema_from_transport=True,
)
Run Code Online (Sandbox Code Playgroud)