I'm trying to fetch all issues in JIRA for all projects. When doing the call one at a time, it works perfect. When trying to run it in a for loop I'm prompted with a 400 Client error.
The way that works:
results = jira_instance.jql("project = FJA", limit = 100, fields=["issuetype", "status", "summary"])
The way that does not work:
projects = ["ADV", "WS", "FJA", "FOIJ", "QW", "UOI"]
for key in projects:
results = jira_instance.jql(f"project = {key})", limit = 100, fields=["issuetype", "status", "summary"])
The error:
Traceback (most recent call last):
File "C:\jira-api-python\jira.py", line 24, in <module>
results = jira_instance.jql("project = {key}", limit = 100, fields=["issuetype", "status", "summary"])
File "C:\.virtualenvs\jira-api-python-rouJrYa4\lib\site-packages\atlassian\jira.py", line 2271, in jql
return self.get("rest/api/2/search", params=params)
File "C:\.virtualenvs\jira-api-python-rouJrYa4\lib\site-packages\atlassian\rest_client.py", line 264, in get
response = self.request(
File "C:\.virtualenvs\jira-api-python-rouJrYa4\lib\site-packages\atlassian\rest_client.py", line 236, in request
response.raise_for_status()
File "C:\.virtualenvs\jira-api-python-rouJrYa4\lib\site-packages\requests\models.py", line 943, in raise_for_status
raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: https://stuff.atlassian.net/rest/api/2/search?startAt=0&maxResults=100&fields=issuetype%2Cstatus%2Csummary&jql=project+%3D+%7Bkey%7D
My guess is that I'm not using the f-string correct. But when I print the value of {key} it is correct.
Any pointers would be greatly appreciated.
Thank you for your time.
Edit:
Added the full traceback, only removed the path to my machine and changed the URL to the endpoint. Below is the full file with credentials and endpoint redacted. The ideas is to create a csv for each project.
The full code:
from atlassian import Jira
import pandas as pd
import time
jira_instance = Jira(
url = "https://stuff.atlassian.net/",
username = "user",
password = "pass",
)
projects = ["ADV", "WS", "FJA", "FOIJ", "QW", "UOI"]
FIELDS_OF_INTEREST = ["key", "fields.summary", "fields.status.name"]
timestamp = time.strftime("%Y%m%d-%H%M%S")
file_ending = ".csv"
for key in projects:
print(f"stuff = {key}")
results = jira_instance.jql(f"project = {key})", limit = 1000, fields=["issuetype", "status", "summary"])
I found the very simple solution.
In this snippet: results = jira_instance.jql(f"project = {key})", limit = 1000, fields=["issuetype", "status", "summary"])
The ) after {key} was not supposed to be there.
Thank you for the help
Related
I am trying to read log analytics in python.
Here is my code:
AZURE_CLIENT_ID = ''
AZURE_CLIENT_SECRET = ''
AZURE_TENANT_ID = ''
workspace_id = ''
from azure.identity import ClientSecretCredential
from datetime import datetime
from azure.monitor.query import LogsQueryClient, LogsQueryStatus
start_time = datetime(2022, 1, 1)
end_time = datetime(2023, 1, 2)
credential = ClientSecretCredential(
client_id = AZURE_CLIENT_ID,
client_secret = AZURE_CLIENT_SECRET,
tenant_id = AZURE_TENANT_ID
)
client = LogsQueryClient(credential)
query = "ContainerLog"
response = client.query_workspace(workspace_id=workspace_id,
query=query, timespan=(start_time, end_time - start_time))
if response.status == LogsQueryStatus.PARTIAL:
error = response.partial_error
print('Results are partial', error.message)
elif response.status == LogsQueryStatus.SUCCESS:
results = []
for table in response.tables:
for row in table.rows:
results.append(dict(zip(table.columns, row)))
print(convert_azure_table_to_dict(results))
and it is failing:
Traceback (most recent call last):
File "c:\temp\x.py", line 24, in <module>
response = client.query_workspace(workspace_id=workspace_id,
File "C:\kourosh\venv\lib\site-packages\azure\core\tracing\decorator.py", line 78, in wrapper_use_tracer
return func(*args, **kwargs)
File "C:\kourosh\venv\lib\site-packages\azure\monitor\query\_logs_query_client.py", line 136, in query_workspace
process_error(err, LogsQueryError)
File "C:\kourosh\venv\lib\site-packages\azure\monitor\query\_helpers.py", line 141, in process_error
raise HttpResponseError(message=error.message, response=error.response, model=model)
azure.core.exceptions.HttpResponseError: (InsufficientAccessError) The provided credentials have insufficient access to perform the requested operation
Code: InsufficientAccessError
Message: The provided credentials have insufficient access to perform the requested operation
I have added Log Analytics API -> Data.Read permission to the registered app that I'm using.
Any idea what is causing this?
Data.Read provides permissions to use the API and grant your app access to your Log Analytics Workspace. However, for accessing data within log analytics workspace, you need to provide permissions as per the data you need access to.
References:
https://learn.microsoft.com/en-us/azure/azure-monitor/logs/api/access-api
https://learn.microsoft.com/en-us/azure/azure-monitor/logs/manage-access?tabs=portal
I was trying to initial and send a proactive message to one Microsoft teams channel with the help of below example:
https://github.com/microsoft/BotBuilder-Samples/tree/master/samples/python/16.proactive-messages
I added this code to the example in order to initiate a message:
connectorClient = await ADAPTER.create_connector_client(service_url=SERVICE_URL)
parameters = ConversationParameters(
is_group=True,
channel_data=CHANNEL_ID,
activity=Activity(type=ActivityTypes.message,
text='Hello World!'),
bot=ChannelAccount(id=BOT_ID),
tenant_id=TENANT_ID)
response = await connectorClient.conversations.create_conversation(parameters)
response.send()
But it didn't work, and I tried many different ways and none of them worked too, always the error is:
Traceback (most recent call last):
File "/home/farid/works/16.proactive-messages/venv/lib/python3.7/site-packages/aiohttp/web_protocol.py", line 418, in start
resp = await task
File "/home/farid/works/16.proactive-messages/venv/lib/python3.7/site-packages/aiohttp/web_app.py", line 458, in _handle
resp = await handler(request)
File "/home/farid/works/16.proactive-messages/app.py", line 103, in notify
raise exception
File "/home/farid/works/16.proactive-messages/app.py", line 100, in notify
await _send_proactive_message()
File "/home/farid/works/16.proactive-messages/app.py", line 152, in _send_proactive_message
response = await connectorClient.conversations.create_conversation(parameters)
File "/home/farid/works/16.proactive-messages/venv/lib/python3.7/site-packages/botframework/connector/aio/operations_async/_conversations_operations_async.py", line 176, in create_conversation
raise models.ErrorResponseException(self._deserialize, response)
botbuilder.schema._models_py3.ErrorResponseException: (BadSyntax) Incorrect conversation creation parameters
I don't know what is my problem here!
Ok, last night Microsoft added a new python sample which solved this issue:
https://github.com/microsoft/BotBuilder-Samples/tree/master/samples/python/58.teams-start-thread-in-channel
There's a very good chance I'm totally off base as I've never tried, before now, to read Python before (I'm a C#/node guy), but it looks like, in your ConversationParameters, you're missing the "Recipient" details (you have the "From", i.e. your Bot, specified), which one does usually need to specify for this.
On the off-chance this helps...
Here is a sample code in C# using sdk 3
var userId = userOrChannelId.Trim();
var botId = context.Activity.Recipient.Id;
var botName = context.Activity.Recipient.Name;
var channelData = context.Activity.GetChannelData<TeamsChannelData>();
var connectorClient = new ConnectorClient(new Uri(context.Activity.ServiceUrl));
var parameters = new ConversationParameters
{
Bot = new ChannelAccount(botId, botName),
Members = !isChannelMessage ? new ChannelAccount[] { new ChannelAccount(userId) } : null,
ChannelData = new TeamsChannelData
{
Tenant = channelData.Tenant,
Channel = isChannelMessage ? new ChannelInfo(userId) : null,
Notification = new NotificationInfo() { Alert = true }
},
IsGroup = isChannelMessage
};
var conversationResource = await connectorClient.Conversations.CreateConversationAsync(parameters);
var replyMessage = Activity.CreateMessageActivity();
replyMessage.From = new ChannelAccount(botId, botName);
replyMessage.Conversation = new ConversationAccount(id: conversationResource.Id.ToString());
replyMessage.ChannelData = new TeamsChannelData() { Notification = new NotificationInfo(true) };
replyMessage.Text = messageText;
if (attachment != null)
replyMessage.Attachments.Add(attachment);
var resourceResponse = await connectorClient.Conversations.SendToConversationAsync(conversationResource.Id, (Activity)replyMessage);
I'm trying to upload some data from rasberry pi to azure iot hub, I'm facing this problem,Where do I need to set the encoding/charsets?
I've tried data.encode('utf-8') something like that but not working.
Might be someone asked, please help me with this specific code.
I'm following this link.
def generate_sas_token():
expiry=3600
ttl = time.time() + expiry
sign_key = "%s\n%d" % ((quote_plus(URI)), int(ttl))
signature = b64encode(HMAC(b64decode(KEY), sign_key, sha256).digest())
rawtoken = {
'sr' : URI,
'sig': signature,
'se' : str(int(ttl))
}
rawtoken['skn'] = POLICY
return 'SharedAccessSignature ' + urlencode(rawtoken)
def send_message(token, message):
url = 'https://{0}/devices/{1}/messages/events?api-version=2016-11-14'.format(URI, IOT_DEVICE_ID)
headers = {
"Content-Type": "application/json",
"Authorization": token
}
data = json.dumps(message)
print(data)
#data.encode('utf-8')
response = requests.post(url, data=data, headers=headers)
if __name__ == '__main__':
# 2. Generate SAS Token
token = generate_sas_token()
# 3. Send Temperature to IoT Hub
while True:
#temp = read_temp()
message = { "temp": str("12") }
send_message(token, message)
time.sleep(1)
And the error is
Traceback (most recent call last):
File "/home/pi/python/test.py", line 45, in <module>
token = generate_sas_token()
File "/home/pi/python/test.py", line 20, in generate_sas_token
signature = b64encode(HMAC(b64decode(KEY), sign_key, sha256).digest())
File "/usr/lib/python3.5/hmac.py", line 84, in __init__
self.update(msg)
File "/usr/lib/python3.5/hmac.py", line 93, in update
self.inner.update(msg)
TypeError: Unicode-objects must be encoded before hashing
The error tells you must encode before creating your HMAC object. It seems you are decoding it first :
HMAC(b64decode(KEY), sign_key, sha256)
A possible solution could be:
HMAC(b64decode(KEY), sign_key.encode('utf-8'), sha256)
I am trying to use the Spotipy method to delete repeat occurrences of a track (so delete duplicates). But the function doesn't seem to work; the Spotify API call is returning an error that there is no authorization token.
Spotify API Return Error:
{
"error": {
"status": 401,
"message": "No token provided"
}
}
Python's Errors:
File "C:\Users\Dylan\Documents\PythonProjects\PlaylistTransfer\Spotify.py", line 87, in remove_all_duplicate_tracks
sp.user_playlist_remove_specific_occurrences_of_tracks(username, playlist_id, tracks)
File "C:\Users\Dylan\Documents\PythonProjects\PlaylistTransfer\venv\lib\site-packages\spotipy\client.py", line 539, in user_playlist_remove_specific_occurrences_of_tracks
payload=payload)
File "C:\Users\Dylan\Documents\PythonProjects\PlaylistTransfer\venv\lib\site-packages\spotipy\client.py", line 183, in _delete
return self._internal_call('DELETE', url, payload, kwargs)
File "C:\Users\Dylan\Documents\PythonProjects\PlaylistTransfer\venv\lib\site-packages\spotipy\client.py", line 124, in _internal_call
headers=r.headers)
spotipy.client.SpotifyException: http status: 400, code:-1 - https://api.spotify.com/v1/___________________________/tracks:
Could not remove tracks, please check parameters.
Here is my code:
def remove_all_duplicate_tracks(playlist_id, token):
sp = spotipy.Spotify(token)
username = get_username(token)
existing_tracks = get_track_uris_for_playlist(playlist_id, token)
duplicate_counter = Counter(existing_tracks)
tracks = []
for uri, count in duplicate_counter.items():
count = count-1
if count > 0:
# hard coded position as 1 for testing...
positions = [1]
#positions = [x for x in range(1, count+1)]
track_dict = {"uri": uri, "positions": positions}
tracks.append(track_dict)
sp.user_playlist_remove_specific_occurrences_of_tracks(username, playlist_id, tracks)
This is what "tracks" contains:
[{'uri': '6jq6rcOikCZAmjliAgAmfT', 'positions': [1]}, {'uri': '3tSmXSxaAnU1EPGKa6NytH', 'positions': [1]}, {'uri': '7jeI6EdY0elPSNz80mAKS8', 'positions': [1]}]
I tested the other methods get_username() and get_track_uris_for_playlist and they return what you'd expect and are working.
Although this answer comes quite late, it is needed because 1) the question is not solved and 2) I believe that it will be helpful to people with a similar problem.
First of all, you should restrict your question to the specific problem, which is the authorization error produced by calling the sp.user_playlist_remove_specific_occurrences_of_tracks() function. This would make the problem more clear. (In the way it is put, one has to dig up the code to find the "hot" spot! Also the details about the tracks just add to the confusion.)
So, I will limit my answer to just the problem and suggest using the following code as a basis:
# Data
username = (your username)
playlist_id = (playlist id) # The ID of the playlist containing the tracks to be deleted
track_ids = [(track_id), (track_id), ...] # List of track IDs to delete
# Authorization process
scope = "playlist-read-private"
token = spotipy.util.prompt_for_user_token(username, scope=scope)
sp = spotipy.Spotify(auth=token)
# Call the track deletion function
sp.user_playlist_remove_all_occurrences_of_tracks(username, playlist_id, track_ids)
I am using this process myself. I have just tried the above code with data of mine and it should also work for you.
You are trying to change user data:
Could not remove tracks, please check parameters.
Pass a valid scope, such as:
playlist-modify-public
playlist-modify-private
More on scopes: https://developer.spotify.com/documentation/general/guides/scopes/
import spotipy
import spotipy.util as util
scope = 'playlist-modify-public playlist-modify-private'
token = util.prompt_for_user_token(username, scope)
if token:
sp = spotipy.Spotify(auth=token)
results = sp.current_user_saved_tracks()
for item in results['items']:
track = item['track']
print track['name'] + ' - ' + track['artists'][0]['name']
else:
print "Can't get token for", username
I am trying to create an instance from a bootable volume in openstack using python-novaclient.
The steps I am taking are following:
Step1: create a volume with an Image "Centos" with 100GB.
Step2: create an instance with the volume that I created in step1.
However, I must be doing something wrong or missing some information that it is not able to complete the task.
Here are my commands in python shell.
import time, getpass
from cinderclient import client
from novaclient.client import Client
project_name = 'project'
region_name = 'region'
keystone_link = 'https://keystone.net:5000/v2.0'
network_zone = "Public"
key_name = 'key_pair'
user = 'user'
pswd = getpass.getpass('Password: ')
# create a connection
cinder = client.Client('1', user, pswd, project_name, keystone_link, region_name = region_name)
# get the volume id that we will attach
print(cinder.volumes.list())
[<Volume: 1d36203e-b90d-458f-99db-8690148b9600>, <Volume: d734f5fc-87f2-41dd-887e-c586bf76d116>]
vol1 = cinder.volumes.list()[1]
vol1.id
block_device_mapping = {'device_name': vol1.id, 'mapping': '/dev/vda'}
### +++++++++++++++++++++++++++++++++++++++++++++++++++++ ###
# now create a connection with nova and create then instance object
nova = Client(2, user, pswd, project_name, keystone_link, region_name = region_name)
# find the image
image = nova.images.find(name="NETO CentOS 6.4 x86_64 v2.2")
# get the flavor
flavor = nova.flavors.find(name="m1.large")
#get the network and attach
network = nova.networks.find(label=network_zone)
nics = [{'net-id': network.id}]
# get the keyname and attach
key_pair = nova.keypairs.get(key_name)
s1 = 'nova-vol-test'
server = nova.servers.create(name = s1, image = image.id, block_device_mapping = block_device_mapping, flavor = flavor.id, nics = nics, key_name = key_pair.name)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/site-packages/novaclient/v1_1/servers.py", line 902, in create
**boot_kwargs)
File "/usr/lib/python2.6/site-packages/novaclient/v1_1/servers.py", line 554, in _boot
return_raw=return_raw, **kwargs)
File "/usr/lib/python2.6/site-packages/novaclient/base.py", line 100, in _create
_resp, body = self.api.client.post(url, body=body)
File "/usr/lib/python2.6/site-packages/novaclient/client.py", line 490, in post
return self._cs_request(url, 'POST', **kwargs)
File "/usr/lib/python2.6/site-packages/novaclient/client.py", line 465, in _cs_request
resp, body = self._time_request(url, method, **kwargs)
File "/usr/lib/python2.6/site-packages/novaclient/client.py", line 439, in _time_request
resp, body = self.request(url, method, **kwargs)
File "/usr/lib/python2.6/site-packages/novaclient/client.py", line 433, in request
raise exceptions.from_response(resp, body, url, method)
novaclient.exceptions.BadRequest: Block Device Mapping is Invalid: failed to get volume /dev/vda. (HTTP 400) (Request-ID: req-2b9db4e1-f24f-48c6-8660-822741ca52ad)
>>>
I tried to find any documentation so that I can solve this on my own, however, I was not able to.
If anyone has tried this before, I would appreciate there help on this.
Thanks,
Murtaza
I was able to get it to work by using this dictionary:
block_dev_mapping = {'vda':'uuid of the volume you want to use'}
I then called it in the create method like this:
instance = nova.servers.create(name="python-test3", image='', block_device_mapping=block_dev_mapping,
flavor=flavor, key_name="my-keypair", nics=nics)