upwork / php-upwork-oauth2

PHP bindings for Upwork API (OAuth2)
Apache License 2.0
10 stars 6 forks source link

Call to a member function getBody() on string #13

Closed dmatora closed 8 months ago

dmatora commented 2 years ago

When server returns 403, API crashes with following exception

  Call to a member function getBody() on string            
bodva commented 1 year ago

Same problem. Is there any plan to fix it?

viperet commented 11 months ago

I also have this problem. When API response with non-200 HTTP code and some JSON with error code - that variable contains string instead of an object and ->getBody() call fails.

bodva commented 9 months ago

@mnovozhylov anything new here? would it be easy with PR?

mnovozhylov commented 9 months ago

Hi @bodva Feel free to create a PR, I'll re-view once I have a chance.

lobodakyrylo commented 8 months ago

I've notice that this happens when you change permissions for your key and then the access token become invalidate.

This happens in this file: vendor/upwork/php-upwork-oauth2/src/Upwork/API/AuthTypes/OAuth2ClientLib.php:103

    try {
        // do not use getParsedResponse, it returns an array
        // but we need a raw json that will be decoded and returned as StdClass object
        $response = $this->getInstance()->getResponse($request);
    } catch (\GuzzleHttp\Exception\ClientException $e) {
        $eResponse = $e->getResponse();
        $response = $eResponse->getBody()->getContents();
    } catch (\Exception $e) {
        $response = $e->getResponseBody();
    }

    ApiDebug::p('got response from server', $response);

    return (string) $response->getBody();

It seems the reason is the $response variable in the last line is not always Response object. For example, $eResponse->getBody()->getContents() returns string.

lobodakyrylo commented 8 months ago

I can't create PR so I created a patch: issue-13-call-getbody-string.patch

mnovozhylov commented 8 months ago

Hi @madlobz Thank you for your contribution! Fixed in v2.4.2.