It seems the query classes in the LogAnalytics module has a token authentication problem. Using the same credentials in Java works without a problem.
#!/usr/bin/python
import sys
sys.path.insert( 0, '/usr/local/lib/python2.7/site-packages' )
from azure.common.credentials import ServicePrincipalCredentials
from azure.loganalytics import LogAnalyticsDataClient
from azure.common.client_factory import get_client_from_cli_profile
from azure.loganalytics.models import QueryBody
import requests
from msrestazure.azure_cloud import AZURE_PUBLIC_CLOUD
import pdb
TENANT_ID = 'ID'
CLIENT = 'ID'
KEY = 'ID'
WORKSPACE_ID = 'ID'
credentials = ServicePrincipalCredentials(
client_id = CLIENT,
secret = KEY,
tenant = TENANT_ID
)
tquery = { "query": "AzureActivity | limit 10", "timespan": "PT12H" }
client = LogAnalyticsDataClient(credentials, base_url=None)
qresult = client.query(WORKSPACE_ID, tquery)
Produces the following error. Checked ServicePrincipal's access permissions and all is good!
Traceback (most recent call last):
File "./azure.py", line 27, in <module>
qresult = client.query(WORKSPACE_ID, tquery)
File "/usr/local/lib/python2.7/site-packages/azure/loganalytics/log_analytics_data_client.py", line 120, in query
raise models.ErrorResponseException(self._deserialize, response)
azure.loganalytics.models.error_response.ErrorResponseException: (InvalidTokenError) The provided authentication is not valid for this resource
Azure Python Module Version
Name: azure
Version: 4.0.0
Summary: Microsoft Azure Client Libraries for Python
Home-page: https://github.com/Azure/azure-sdk-for-python
Author: Microsoft Corporation
Author-email: [email protected]
License: MIT License
Location: /usr/local/lib/python2.7/site-packages
Requires: azure-graphrbac, azure-servicemanagement-legacy, azure-keyvault, azure-mgmt, azure-servicebus, azure-storage-queue, azure-datalake-store, azure-storage-file, azure-cosmosdb-table, azure-storage-blob, azure-batch, azure-eventgrid, azure-loganalytics, azure-applicationinsights, azure-servicefabric
Required-by:
Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @kshitizm
Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @kshitizm
@phspies Try this, worked for me:
```
credentials = ServicePrincipalCredentials(
client_id = application_id,
secret = application_secret,
tenant = tenant_id,
resource = 'https://api.loganalytics.io'
)
Hey @karanotts . I have this issue for UserPassCredentials. My issue is raised here . What is the resolution for this ?
Thanks
@karanotts, thank you it worked. Will be good to get the documentation updated with this vital piece of information.
Most helpful comment
@phspies Try this, worked for me:
```
credentials = ServicePrincipalCredentials(
client_id = application_id,
secret = application_secret,
tenant = tenant_id,
resource = 'https://api.loganalytics.io'
)