需要帮助以在Python中使用LinkedIn API v2(oauth2)

时间:2018-08-31 10:34:13

标签: python oauth-2.0 linkedin-api

我在https://github.com/ozgur/python-linkedin中使用了带有ID和密钥的代码。如果我运行此代码并转到localhost:8080(我使用此端口),则会收到以下异常:

127.0.0.1 - - [31/Aug/2018 13:25:49] "GET / HTTP/1.1" 200 -
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 65176)
Traceback (most recent call last):
  File "C:\Users\osads\AppData\Local\Programs\Python\Python37-32\lib\socketserver.py", line 647, in process_request_thread
    self.finish_request(request, client_address)
  File "C:\Users\osads\AppData\Local\Programs\Python\Python37-32\lib\socketserver.py", line 357, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "C:\Users\osads\AppData\Local\Programs\Python\Python37-32\lib\http\server.py", line 646, in __init__
    super().__init__(*args, **kwargs)
  File "C:\Users\osads\AppData\Local\Programs\Python\Python37-32\lib\socketserver.py", line 717, in __init__
    self.handle()
  File "C:\Users\osads\AppData\Local\Programs\Python\Python37-32\lib\http\server.py", line 426, in handle
    self.handle_one_request()
  File "C:\Users\osads\AppData\Local\Programs\Python\Python37-32\lib\http\server.py", line 414, in handle_one_request
    method()
  File "C:/Users/osads/PycharmProjects/python-linkedin-master/examples/http_api.py", line 60, in do_GET
    open_new_tab(liw.authentication.authorization_url)
  File "C:\Users\osads\PycharmProjects\python-linkedin-master\linkedin\linkedin.py", line 101, in authorization_url
    qsl = ['%s=%s' % (quote(k), quote(v)) for k, v in qd.items()]
  File "C:\Users\osads\PycharmProjects\python-linkedin-master\linkedin\linkedin.py", line 101, in <listcomp>
    qsl = ['%s=%s' % (quote(k), quote(v)) for k, v in qd.items()]
  File "C:\Users\osads\AppData\Local\Programs\Python\Python37-32\lib\urllib\parse.py", line 791, in quote
    return quote_from_bytes(string, safe)
  File "C:\Users\osads\AppData\Local\Programs\Python\Python37-32\lib\urllib\parse.py", line 816, in quote_from_bytes
    raise TypeError("quote_from_bytes() expected bytes")
TypeError: quote_from_bytes() expected bytes

该如何解决?

UPD:来自Linkedin.py的代码

 @property
    def authorization_url(self):
        qd = {'response_type': 'code',
              'client_id': self.key,
              'scope': (' '.join(self.permissions)).strip(),
              'state': self.state or self._make_new_state(),
              'redirect_uri': self.redirect_uri}
        # urlencode uses quote_plus when encoding the query string so,
        # we ought to be encoding the qs by on our own.
        qsl = ['%s=%s' % (quote(k), quote(v)) for k, v in qd.items()]
        return '%s?%s' % (self.AUTHORIZATION_URL, '&'.join(qsl))

1 个答案:

答案 0 :(得分:0)

此项的值

'state': self.state or self._make_new_state()

是布尔值。 quote需要一个字符串。

简而言之,这是您的错误:

from urllib.parse import quote
quote(True)

我不知道您到底在做什么,但这可以解决您的错误(并且可能会产生一些新错误)。

'state': str(self.state or self._make_new_state())