Create project via REST with python

mon_nom November 12, 2018

Hi everyone,

I need help. I am new to Jira, new to python but not new in development.

I need to create a project via REST in python, I am using flask framework.

Could you please guide me through pseudo code on the different steps I need to do?

My configuration: I am working on a CentOs platform via ssh.

Would it be something like:

1. Authentication to my Jira software installation

2. Connection to the Jira REST Api?

I am not sure if this documentation applies to me: https://developer.atlassian.com/cloud/jira/platform/rest/v3/#api-api-3-project-post

Many thanks for your help!

 

1 answer

0 votes
Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
November 13, 2018

Are you using Jira Cloud or Jira Server?  

I ask that first because the steps needed to make this work can be slightly different between the platforms.  You can typically tell by looking at the URL of your Jira site.  If you have an atlassian.net or jira.com domain, then these are hosted by Atlassian and in turn are Atlassian Cloud products.  Whereas if you have a URL that is on your own domain, you're probably using Jira server.

If you are using Jira Server, let me know and I can provide some more detailed steps. But I'm going to move ahead with the Jira Cloud steps because of the link you cited.

 

For Jira Cloud, I would recommend first following this guide to create an API token: 

https://confluence.atlassian.com/cloud/api-tokens-938839638.html

There are other methods you can use to authenticate that are explained in https://developer.atlassian.com/cloud/jira/platform/jira-rest-api-basic-authentication/

But the use of basic authentication will likely be deprecated in the near future, so it makes sense to take the time to create the token instead.   Once you have that created, then you can actually make REST calls to your Jira Cloud site in a variety of different methods, curl, python, php, node.js, etc.  Personally I prefer to use curl to make REST calls to my Cloud site, but to each his own.

The document you linked to would apply here, but only if you're using Jira Cloud.  If you're using Jira Server, there is a similar but different reference guide to follow.  If you go to your link, you can scroll down to the examples section and then click the python tab to see an example payload you use to make a rest call for this endpoint such as:

# This code sample uses the 'requests' library:
# http://docs.python-requests.org
import requests
import json

url = "https://example.atlassian.net/rest/api/3/project"

headers = {
"Accept": "application/json",
"Content-Type": "application/json",
"Bearer": "<access_token>"
}

payload = json.dumps( {
"key": "EX",
"name": "Example",
"projectTypeKey": "business",
"projectTemplateKey": "com.atlassian.jira-core-project-templates:jira-core-simplified-project-management",
"description": "Example Project description",
"leadAccountId": "bd429c95-e27b-4423-a0bd-421cf3d69129",
"url": "http://atlassian.com",
"assigneeType": "PROJECT_LEAD",
"avatarId": 10200,
"issueSecurityScheme": 10001,
"permissionScheme": 10011,
"notificationScheme": 10021,
"categoryId": 10120 } )
response = requests.request( "POST", url, data=payload, headers=headers
)
print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))

In this case, before trying to send this payload out, you will probably want to first make a few REST API get calls to get back information on other projects.  This can be used as a template for sections such as permissionScheme, notificationScheme, etc.  You'll need to assign these when creating a project via REST, and if you don't know which id numbers correspond to which schemes, you probably can't create the project.

It might help to first do a few GET calls just to gather some data from your instance first such as with a 

# This code sample uses the 'requests' library:
# http://docs.python-requests.org

import requests
import json

url = "https://your-domain.atlassian.net/rest/api/3/project/{projectIdOrKey}"

headers = {
"Accept": "application/json",
"Bearer": "<access_token>"
}
response = requests.request( "GET", url, headers=headers )

print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))

mon_nom November 19, 2018

THANK YOU for your answer and sorry for the late reply.

I installed Jira on my own server. I tried to do few simple firsts GET requests and I didn't succeed I guess because of the authentication that I may be doing wrong.

I will try the simple GET request you sent me but I think I need to be already authenticated. Sorry if my question is a very newbie question but how do I get the "access_token"?


Many thanks

Andy Heinzer
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
November 19, 2018

Ah, ok.  For server, it's a bit different.   In Jira Server, you can typically just use basic authentication (username and password).  This is described in more detail in https://developer.atlassian.com/server/jira/platform/basic-authentication/

mon_nom November 21, 2018

Thanks for your help, In the link you sent me there is a link to an OAuth authentication tutorial which I am following.

Do you know about OAuth with Jira.

I am almost done but I am stuck with an error when trying to execute the python script:

class SignatureMethod_RSA_SHA1(oauth.SignatureMethod):
AttributeError: 'module' object has no attribute 'SignatureMethod'

I think the oauth2 package is not correctly installed.

Could you please help me?

mon_nom November 22, 2018

Fixed my problem, I installed the wrong package......

For the OAuth tutorial with python example:

do not install python-oauth2 package, install oauth2 package

Suggest an answer

Log in or Sign up to answer