colab を使って Jira cloud をOAuth で接続
背景
colab から、Jira に接続したい。
方法
RSAのカギを作る。
次のサイトの情報に従って、鍵を作成する。
$ openssl genrsa -out jira.pem 1024 $ openssl rsa -in jira.pem -pubout -out jira.pub
アプリケーションリンクを作る。
次の方法(Step1)に従ってダミーのアプリケーションリンクを作る。 ただしアプリケーションのリンクは、「歯車」→「製品」→「アプリケーションのリンク」と辿る。
ここで先 jira.pub
の内容をコピペする。
colab 用にコードをコピペする。
次のサイトの回答をコピペする。
コピペすべき内容をこちらにも貼っておく。
!pip install jira
from oauthlib.oauth1 import SIGNATURE_RSA from requests_oauthlib import OAuth1Session from jira.client import JIRA # The Consumer Key created while setting up the "Incoming Authentication" in # JIRA for the Application Link. CONSUMER_KEY = 'oauth-sample-consumer' CONSUMER_SECRET = 'dont_care' VERIFIER = 'jira_verifier' # The contents of the rsa.pem file generated (the private RSA key) RSA_KEY = """ -----BEGIN RSA PRIVATE KEY----- -----END RSA PRIVATE KEY----- """ # The URLs for the JIRA instance JIRA_SERVER = 'https://YOUR_JIRA_SERVER' REQUEST_TOKEN_URL = JIRA_SERVER + '/plugins/servlet/oauth/request-token' AUTHORIZE_URL = JIRA_SERVER + '/plugins/servlet/oauth/authorize' ACCESS_TOKEN_URL = JIRA_SERVER + '/plugins/servlet/oauth/access-token' # Step 1: Get a request token oauth = OAuth1Session(CONSUMER_KEY, client_secret= CONSUMER_SECRET, signature_method=SIGNATURE_RSA, rsa_key=RSA_KEY) request_token = oauth.fetch_request_token(REQUEST_TOKEN_URL) resource_owner_key = request_token['oauth_token']; resource_owner_secret = request_token['oauth_token_secret']; print("STEP 1: GET REQUEST TOKEN") print(" oauth_token={}".format(resource_owner_key)) print(" oauth_token_secret={}".format(resource_owner_secret)) print("\n") # Step 2: Get the end-user's authorization print("STEP2: AUTHORIZATION") print(" Visit to the following URL to provide authorization:") print(" {}?oauth_token={}".format(AUTHORIZE_URL, request_token['oauth_token'])) print("\n")
ここで、colab では、別のコードを一旦切って、
STEP2: AUTHORIZATION Visit to the following URL to provide authorization:
と表示されるリンクをクリックし、許可をする。 その後、次のコードとして実行する。
oauth = OAuth1Session(CONSUMER_KEY, client_secret= CONSUMER_SECRET, resource_owner_key=resource_owner_key, resource_owner_secret=resource_owner_secret, verifier=VERIFIER, signature_method=SIGNATURE_RSA, rsa_key=RSA_KEY) # Step 3: Get the access token access_token = oauth.fetch_access_token(ACCESS_TOKEN_URL) print("STEP2: GET ACCESS TOKEN") print(" oauth_token={}".format(access_token['oauth_token'])) print(" oauth_token_secret={}".format(access_token['oauth_token_secret'])) print("\n") # Now you can use the access tokens with the JIRA client. Hooray! jira = JIRA(options={'server': JIRA_SERVER}, oauth={ 'access_token': access_token['oauth_token'], 'access_token_secret': access_token['oauth_token_secret'], 'consumer_key': CONSUMER_KEY, 'key_cert': RSA_KEY }) # print all of the project keys just as an exmaple for project in jira.projects(): print(project.key)
まとめ
ドキュメントが古いのか嘘が書いてあったりして難しいがなんとか上記の方法でできる(2019/07/17現在)