Getting 401 on Twitter OAuth POST requests

前端 未结 6 1327
星月不相逢
星月不相逢 2020-12-18 05:27

I am trying to use Twitter OAuth and my POST requests are failing with a 401 (Invalid OAuth Request) error.

For example, if I

6条回答
  •  死守一世寂寞
    2020-12-18 06:01

    I second the answer by Jrgns. I has exactly the same issue. When reading the example Twitter provides, it's actually clear. However their pseudo code is misleading. In Python this worked for me :

    def encodekeyval(key, val):
        key = urllib.quote(key, '')
        val = urllib.quote(val, '')
        return urllib.quote(key + '=' + val, '')
    
    def signature_base_string(urlstr, oauthdata):
        sigstr = 'POST&' + urllib.quote(urlstr,'') + '&'
        # retrieve "post" data as dictionary of name value pairs
        pdata = oauthdata.getpdata()
        # need to sort parameters
        pstr = '%26'.join([encodekeyval(key, pdata[key]) for key in sorted(pdata.keys())])
        return sigstr + pstr
    

提交回复
热议问题