Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Python script to upload attachment into Confluence

Swaroop Krishna Rao
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 11, 2019

I am using the script python script to upload an attachment into Confluence. 

 

#!/usr/bin/python

from __future__ import with_statement
import sys, string, xmlrpclib, re, os

if len(sys.argv) < 5:
exit("Usage: " + sys.argv[0] + " spacekey pagetitle contentType filename");

spacekey = sys.argv[1];
pagetitle = sys.argv[2];
contentType = sys.argv[3];
filename = sys.argv[4];

cwd = os.getcwd() # Get the current working directory (cwd)
files = os.listdir(cwd) # Get all the files in that directory
print("Files in '%s': %s" % (cwd, files))

THIS_FOLDER = os.path.dirname(os.path.abspath(__file__))
filename = os.path.join(THIS_FOLDER, 'help_en.pdf')

with open(filename, 'rb') as f:
data = f.read(); # slurp all the data

server = xmlrpclib.ServerProxy('BaseURL');
token = server.confluence1.login('Username', 'Password');
page = server.confluence2.getPage(token, spacekey, pagetitle);
if page is None:
exit("Could not find page " + spacekey + ":" + pagetitle);

attachment = {};
attachment['fileName'] = os.path.basename(filename);
attachment['contentType'] = contentType;

server.confluence1.addAttachment(token, page['id'], attachment, xmlrpclib.Binary(data));



I am getting the following error: 

xmlrpclib.Fault: <Fault 0: 'java.lang.Exception: com.atlassian.confluence.rpc.AuthenticationFailedException: Attempt to log in user failed. The maximum number of failed login attempts has been reached. Please log into the web application through the web interface to reset the number of failed login attempts.'>


Is there any solution to this and is there any other python script with different API?

1 answer

0 votes
Dominic Lagger
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 13, 2019

Hi @Swaroop Krishna Rao 

The error which is given is pretty clear I think. 

You have tried and failed to login too much. Do you have the possibility to login to the web application and reset the failed login attempts?

Swaroop Krishna Rao
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
March 15, 2019

@Dominic Lagger 

I tried login to the web application and it was successful.

 

But now I am getting

 

Traceback (most recent call last):

      page = server.confluence2.getPage(token, spacekey, pagetitle);

 

xmlrpclib.Fault: <Fault 0: "java.lang.Exception: com.atlassian.confluence.rpc.RemoteException: You're not allowed to view that page, or it does not exist.">


I have defined the token, spacekey and pagetitle.

Dominic Lagger
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 17, 2019

Hi @Swaroop Krishna Rao 

First, I like that you were successful to login. 

To the second error you get. For me, it seems to be a permission thing. Perhaps you do not have the permission to view this page. On the other hand, it might be a special key in the pagetitle. 

  • Can you see (view/edit) the page in the web application? 
  • Does the pagetitle have any special key in it? 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events