Hello! It is necessary to publish an article in the Telegra.ph service using Python. Tried to do this with the "telegraph" and "python-telegraphapi" modules, but without success. I tried to just run the code that is attached to the "telegraph" module instructions, as a sample:

from telegraph import Telegraph telegraph = Telegraph() telegraph.create_account(short_name='1337') response = telegraph.create_page( 'Hey', html_content='<p>Hello, world!</p>' ) print('http://telegra.ph/{}'.format(response['path'])) 

Produces this error:

 File "AutoContent.py", line 6, in <module> html_content='<p>Hello, world!</p>' File "C:\Program Files\Python36\lib\site-packages\telegraph\api.py", line 168, in create_page 'return_content': return_content File "C:\Program Files\Python36\lib\site-packages\telegraph\api.py", line 40, in method raise TelegraphException(response.get('error')) telegraph.exceptions.TelegraphException: PAGE_SAVE_FAILED 

I tried the sample code of the module "python-telegraphapi":

 from telegraphapi import Telegraph telegraph = Telegraph() telegraph.createAccount("PythonTelegraphAPI") page = telegraph.createPage("Hello world!", html_content="<b>Welcome, TelegraphAPI!</b>") print('http://telegra.ph/{}'.format(page['path'])) 

It turns out this error:

 File "AutoContent.py", line 4, in <module> page = telegraph.createPage("Hello world!", html_content="<b>Welcome, TelegraphAPI!</b>") File "C:\Program Files\Python36\lib\site-packages\telegraphapi\main.py", line 139, in createPage "return_content": return_content File "C:\Program Files\Python36\lib\site-packages\telegraphapi\main.py", line 32, in make_method post_request.json()['error']) telegraphapi.exceptions.TelegraphAPIException: Error while executing createPage: PAGE_SAVE_FAILED 

I do not even know where to look to solve the problem. Please, help!

    1 answer 1

    The example with the telegraph module works as is. It is easy to take advantage of the Telegraph API . To create a new page, you need access_token to get an access_token , you can send a createAccount request:

     # get :access-token POST https://api.telegra.ph/createAccount?short_name=не важно 

    access_token value from the server access_token , you can publish a new article at https://telegra.ph

     # create page :access-token = b968da509bb76866c35425099bc0989a5ec3b32997d55286c657e6994bbb POST https://api.telegra.ph/createPage access_token=:access-token&title=Sample Page&content=[{"tag":"p","children":["Hello, world!"]}]&return_content=true 

    requests use emacs' restclient syntax

    On Python it might look like:

     <script src="https://cdn.rawgit.com/brython-dev/brython/3.4.0/www/src/brython.js"></script><script src="https://cdn.rawgit.com/brython-dev/brython/3.4.0/www/src/brython_stdlib.js"></script><body onload="brython()"><script type="text/python"> #!/usr/bin/env python3 import json from urllib.request import urlopen from urllib.parse import urlencode def make_request(method, **params): url = 'https://api.telegra.ph/' + method params = {k: v if isinstance(v, str) else json.dumps(v) for k, v in params.items()} r = json.loads(urlopen(url + '?' + urlencode(params)).read()) if not r['ok']: raise ValueError(str(r)) return r['result'] def get_access_token(short_name): return make_request('createAccount', short_name=short_name)['access_token'] def create_page(**params): return make_request('createPage', **params) # try your own input from browser import document, html @document["mybutton"].bind("click") def on_click(event): token = get_access_token(document["short_name"].value) title = document["title"].value page = create_page(access_token=token, title=title, content=document["content"].value) print(page['url']) document <= html.P(html.A(title, href=page['url'])) </script> <div><label for="short_name">Имя: <input id="short_name" value="SO client"></div> <div><label for="title">Заголовок: <input id="title" value="Привет"></div> <div><label for="content">Элементы статьи: <textarea id="content" cols=30>[{"tag": "p", "children": ["Здравствуй Мир!"]}]</textarea></div> <div><button id="mybutton">Запустить</button></div> 

    • Very grateful for the code! But I cannot understand in what form one should write the text to be published, i.e. I can not understand this: [{"tag": "p", "children": ["Hello World!"]}]. Could you explain this? And I still can not understand the short_name parameter, with the SO client value in your example. Why is it needed? - hohokibeza
    • The @hohokibeza code is shown to show the "how it works" picture. In fact, use more friendly wrappers like the tetelgraph type module. For example, you can transfer the content as html (the telegraph module turns the expected api.telegra.ph service into input for you) See the code in question ¶ If you don’t know what short_name is, then you don’t need it (substitute any value). - jfs
    • but the code from the question gives an error. Please, could you show the working publication code of the article on the Telegraph and the output of a link to this article using the telegraph module? I would be very grateful to you! - hohokibeza
    • @hohokibeza: the words in the response "works as it is" means that I copied the code as it was, launched it, and it successfully completed The analogue of this code is given in the answer, which can be run directly in the browser by clicking the "Run code" button. If this code is run too many times with the same parameters, it can return an error. The solution is simple: change the parameters and do not run too often. - jfs
    • Your code works for me, but not from the telegraph module. Previously, I gave out telegraph.exceptions.TelegraphException: PAGE_SAVE_FAILED (I wrote a question), now I launch it - it generally gives ImportError: cannot import name 'Telegraph' , although the telegraph module is installed. You do not know the reason for both errors? I run the example code from here , I don’t change it in any way. - hohokibeza