valknight / Cohost.py

A python library for Cohost.org!
MIT License
58 stars 8 forks source link

403 error if page is private, but post is still made #22

Open pieartsy opened 1 year ago

pieartsy commented 1 year ago

I get an exception and 403 error saying "Something went wrong." if I try to post to a private page, but the post still is made and visible on the cohost site.

from cohost.models.user import User
from cohost.models.block import AttachmentBlock, MarkdownBlock

import os
user = User.login(os.getenv("COHOST_USER"), os.getenv("COHOST_PASS"))
project = user.getProject('myPage')

blocks=[MarkdownBlock('test')]

newPost = project.post(headline="test", blocks=blocks) # this is where the exception is raised

print('Check out your post at {}'.format(newPost.url))

image

valknight commented 1 year ago

Oof, that's nasty, sorry about that. Do you happen to have a callstack on hand? This is a fairly solid repro as it is, but, knowing exactly which line would be super handy.

Thank you for reporting! 🥳

pieartsy commented 1 year ago

Is this what you mean? If not, let me know

File "C:\Users\me\Documents\GitHub\cohost-poetry-bot\error.py", line 12, in <module>
    newPost = project.post(headline="test", blocks=blocks) # this is where the exception is raised
  File "c:\Users\me\Documents\GitHub\cohost-poetry-bot\poetryvenv\lib\site-packages\cohost\models\project.py", line 173, in post
    return self.getPosts()[0]  # this will be what we just posted
  File "c:\Users\me\Documents\GitHub\cohost-poetry-bot\poetryvenv\lib\site-packages\cohost\models\project.py", line 81, in getPosts
    postData = self.getPostsRaw(page)
  File "c:\Users\me\Documents\GitHub\cohost-poetry-bot\poetryvenv\lib\site-packages\cohost\models\project.py", line 75, in getPostsRaw
    return fetch('get',
  File "c:\Users\me\Documents\GitHub\cohost-poetry-bot\poetryvenv\lib\site-packages\cohost\network.py", line 58, in fetch
    raise Exception(res)
Exception: {'status': 403, 'message': 'Something went wrong.'}
valknight commented 1 year ago

That's perfect, thanks! I'll take a look at what's going on here now 🕵️

valknight commented 1 year ago

Ok, this is fascinating - this doesn't seem to be reproducing on my machine. I've attached a screenshot showing this working, and I've copied below the script I'm using.

Would it be possible for you to give this a run and let me know if you're still seeing this issue?

Screenshot 2023-04-02 at 19 24 48
import os
from cohost.models.user import User
from cohost.models.block import AttachmentBlock, MarkdownBlock

def main():
    print('logging in... ')
    username = os.environ.get('cohostUser')
    password = os.environ.get('cohostPass')
    handle = os.environ.get('cohostHandle')
    if username is None:
        username = input('username: ')
    if password is None:
        password = input('password: ')
    if handle is None:
        handle = input('handle: ')
    user = User.login(username, password)
    project = user.getProject(handle)

    # Begin test!
    blocks=[MarkdownBlock('test')]
    # this is where the exception is raised
    newPost = project.post(headline="test", blocks=blocks) 
    # If we reach this point, no exception was raised.
    print('Check out your post at {}'.format(newPost.url))
    print('test post made successfully')
    return

if __name__ == '__main__':
    main()
valknight commented 1 year ago

Also on this - would it be OK to ask at what rate are you making / retrieving posts? Based on the error code being 403 when retrieving posts, this could indicate rate limiting, or some other weird issue with Cohost.

(Cannot wait for official API in cases like this 😆 )

pieartsy commented 1 year ago

Hmm, it might be rate limiting then. I tried running it once and it didn't happen (though something else weird happened instead...*), and running it the second time raised the exception. I'm not posting from the bot that often, just once every 24 hours, but in the beginning when i was testing it I was posting a lot. Not sure why it just seems to happen for private posts though? I'm not sure...

*basically I was sure I had set the page to private, but when I posted it, everything had switched back to not being private...not sure if I just hallucinated or misremembered

valknight commented 1 year ago

That's super odd. And yeah, when I've tested previously, Cohost has been pretty light on rate limiting. The only time I've seen similar is when our user agent got caught by Cloudflare as suspicious.

My best guess is if it isn't happening now, this might be tied to cohost moving away from Cloudflare to a different DDOS protection service.

I'll leave this issue open though, incase anyone in future runs into similar!