romanzipp / Laravel-Twitch

Twitch Helix API PHP Wrapper for Laravel
https://packagist.org/packages/romanzipp/laravel-twitch
MIT License
108 stars 18 forks source link

How to get the oauth token from getOAuthToken() #36

Closed JasonRivers closed 5 years ago

JasonRivers commented 5 years ago

How do I get the OAuth token with a given "code"? The "token" response here is NULL

I'm not sure how I'm supposed to parse the data to get the token out?

From within Laravel I'm doing the following:

        twitch::setClientId('REDACTED');
        twitch::setClientSecret('REDACTED');
        $r=twitch::getOAuthToken($code);

        var_dump($r);

The response is:


object(romanzipp\Twitch\Result)#360 (9) {
  ["success"]=>
  bool(true)
  ["exception"]=>
  NULL
  ["data"]=>
  array(0) {
  }
  ["total"]=>
  int(0)
  ["status"]=>
  int(200)
  ["pagination"]=>
  NULL
  ["paginator"]=>
  object(romanzipp\Twitch\Helpers\Paginator)#344 (2) {
    ["pagination":"romanzipp\Twitch\Helpers\Paginator":private]=>
    NULL
    ["action"]=>
    NULL
  }
  ["response"]=>
  object(GuzzleHttp\Psr7\Response)#355 (6) {
    ["reasonPhrase":"GuzzleHttp\Psr7\Response":private]=>
    string(2) "OK"
    ["statusCode":"GuzzleHttp\Psr7\Response":private]=>
    int(200)
    ["headers":"GuzzleHttp\Psr7\Response":private]=>
    array(7) {
      ["Date"]=>
      array(1) {
        [0]=>
        string(29) "Mon, 02 Sep 2019 19:41:34 GMT"
      }
      ["Content-Type"]=>
      array(1) {
        [0]=>
        string(16) "application/json"
      }
      ["Content-Length"]=>
      array(1) {
        [0]=>
        string(3) "229"
      }
      ["Connection"]=>
      array(1) {
        [0]=>
        string(10) "keep-alive"
      }
      ["Server"]=>
      array(1) {
        [0]=>
        string(12) "nginx/1.14.1"
      }
      ["Access-Control-Allow-Origin"]=>
      array(1) {
        [0]=>
        string(1) "*"
      }
      ["X-Ctxlog-Logid"]=>
      array(1) {
        [0]=>
        string(35) "1-5d6d706e-3de0be8f72fd6342123a563e"
      }
    }
    ["headerNames":"GuzzleHttp\Psr7\Response":private]=>
    array(7) {
      ["date"]=>
      string(4) "Date"
      ["content-type"]=>
      string(12) "Content-Type"
      ["content-length"]=>
      string(14) "Content-Length"
      ["connection"]=>
      string(10) "Connection"
      ["server"]=>
      string(6) "Server"
      ["access-control-allow-origin"]=>
      string(27) "Access-Control-Allow-Origin"
      ["x-ctxlog-logid"]=>
      string(14) "X-Ctxlog-Logid"
    }
    ["protocol":"GuzzleHttp\Psr7\Response":private]=>
    string(3) "1.1"
    ["stream":"GuzzleHttp\Psr7\Response":private]=>
    object(GuzzleHttp\Psr7\Stream)#353 (7) {
      ["stream":"GuzzleHttp\Psr7\Stream":private]=>
      resource(274) of type (stream)
      ["size":"GuzzleHttp\Psr7\Stream":private]=>
      NULL
      ["seekable":"GuzzleHttp\Psr7\Stream":private]=>
      bool(true)
      ["readable":"GuzzleHttp\Psr7\Stream":private]=>
      bool(true)
      ["writable":"GuzzleHttp\Psr7\Stream":private]=>
      bool(true)
      ["uri":"GuzzleHttp\Psr7\Stream":private]=>
      string(10) "php://temp"
      ["customMetadata":"GuzzleHttp\Psr7\Stream":private]=>
      array(0) {
      }
    }
  }
  ["twitch"]=>
  object(romanzipp\Twitch\Twitch)#329 (6) {
    ["client":protected]=>
    object(GuzzleHttp\Client)#324 (1) {
      ["config":"GuzzleHttp\Client":private]=>
      array(8) {
        ["base_uri"]=>
        object(GuzzleHttp\Psr7\Uri)#342 (7) {
          ["scheme":"GuzzleHttp\Psr7\Uri":private]=>
          string(5) "https"
          ["userInfo":"GuzzleHttp\Psr7\Uri":private]=>
          string(0) ""
          ["host":"GuzzleHttp\Psr7\Uri":private]=>
          string(13) "api.twitch.tv"
          ["port":"GuzzleHttp\Psr7\Uri":private]=>
          NULL
          ["path":"GuzzleHttp\Psr7\Uri":private]=>
          string(7) "/helix/"
          ["query":"GuzzleHttp\Psr7\Uri":private]=>
          string(0) ""
          ["fragment":"GuzzleHttp\Psr7\Uri":private]=>
          string(0) ""
        }
        ["handler"]=>
        object(GuzzleHttp\HandlerStack)#325 (3) {
          ["handler":"GuzzleHttp\HandlerStack":private]=>
          object(Closure)#337 (2) {
            ["static"]=>
            array(2) {
              ["default"]=>
              object(Closure)#335 (2) {
                ["static"]=>
                array(2) {
                  ["default"]=>
                  object(GuzzleHttp\Handler\CurlMultiHandler)#331 (5) {
                    ["factory":"GuzzleHttp\Handler\CurlMultiHandler":private]=>
                    object(GuzzleHttp\Handler\CurlFactory)#332 (2) {
                      ["handles":"GuzzleHttp\Handler\CurlFactory":private]=>
                      array(0) {
                      }
                      ["maxHandles":"GuzzleHttp\Handler\CurlFactory":private]=>
                      int(50)
                    }
                    ["selectTimeout":"GuzzleHttp\Handler\CurlMultiHandler":private]=>
                    int(1)
                    ["active":"GuzzleHttp\Handler\CurlMultiHandler":private]=>
                    NULL
                    ["handles":"GuzzleHttp\Handler\CurlMultiHandler":private]=>
                    array(0) {
                    }
                    ["delays":"GuzzleHttp\Handler\CurlMultiHandler":private]=>
                    array(0) {
                    }
                  }
                  ["sync"]=>
                  object(GuzzleHttp\Handler\CurlHandler)#333 (1) {
                    ["factory":"GuzzleHttp\Handler\CurlHandler":private]=>
                    object(GuzzleHttp\Handler\CurlFactory)#334 (2) {
                      ["handles":"GuzzleHttp\Handler\CurlFactory":private]=>
                      array(1) {
                        [0]=>
                        resource(276) of type (curl)
                      }
                      ["maxHandles":"GuzzleHttp\Handler\CurlFactory":private]=>
                      int(3)
                    }
                  }
                }
                ["parameter"]=>
                array(2) {
                  ["$request"]=>
                  string(10) "<required>"
                  ["$options"]=>
                  string(10) "<required>"
                }
              }
              ["streaming"]=>
              object(GuzzleHttp\Handler\StreamHandler)#336 (1) {
                ["lastHeaders":"GuzzleHttp\Handler\StreamHandler":private]=>
                array(0) {
                }
              }
            }
            ["parameter"]=>
            array(2) {
              ["$request"]=>
              string(10) "<required>"
              ["$options"]=>
              string(10) "<required>"
            }
          }
          ["stack":"GuzzleHttp\HandlerStack":private]=>
          array(4) {
            [0]=>
            array(2) {
              [0]=>
              object(Closure)#338 (1) {
                ["parameter"]=>
                array(1) {
                  ["$handler"]=>
                  string(10) "<required>"
                }
              }
              [1]=>
              string(11) "http_errors"
            }
            [1]=>
            array(2) {
              [0]=>
              object(Closure)#339 (1) {
                ["parameter"]=>
                array(1) {
                  ["$handler"]=>
                  string(10) "<required>"
                }
              }
              [1]=>
              string(15) "allow_redirects"
            }
            [2]=>
            array(2) {
              [0]=>
              object(Closure)#340 (1) {
                ["parameter"]=>
                array(1) {
                  ["$handler"]=>
                  string(10) "<required>"
                }
              }
              [1]=>
              string(7) "cookies"
            }
            [3]=>
            array(2) {
              [0]=>
              object(Closure)#341 (1) {
                ["parameter"]=>
                array(1) {
                  ["$handler"]=>
                  string(10) "<required>"
                }
              }
              [1]=>
              string(12) "prepare_body"
            }
          }
          ["cached":"GuzzleHttp\HandlerStack":private]=>
          object(Closure)#351 (2) {
            ["static"]=>
            array(1) {
              ["handler"]=>
              object(GuzzleHttp\RedirectMiddleware)#350 (1) {
                ["nextHandler":"GuzzleHttp\RedirectMiddleware":private]=>
                object(Closure)#349 (2) {
                  ["static"]=>
                  array(1) {
                    ["handler"]=>
                    object(GuzzleHttp\PrepareBodyMiddleware)#346 (1) {
                      ["nextHandler":"GuzzleHttp\PrepareBodyMiddleware":private]=>
                      object(Closure)#337 (2) {
                        ["static"]=>
                        array(2) {
                          ["default"]=>
                          object(Closure)#335 (2) {
                            ["static"]=>
                            array(2) {
                              ["default"]=>
                              object(GuzzleHttp\Handler\CurlMultiHandler)#331 (5) {
                                ["factory":"GuzzleHttp\Handler\CurlMultiHandler":private]=>
                                object(GuzzleHttp\Handler\CurlFactory)#332 (2) {
                                  ["handles":"GuzzleHttp\Handler\CurlFactory":private]=>
                                  array(0) {
                                  }
                                  ["maxHandles":"GuzzleHttp\Handler\CurlFactory":private]=>
                                  int(50)
                                }
                                ["selectTimeout":"GuzzleHttp\Handler\CurlMultiHandler":private]=>
                                int(1)
                                ["active":"GuzzleHttp\Handler\CurlMultiHandler":private]=>
                                NULL
                                ["handles":"GuzzleHttp\Handler\CurlMultiHandler":private]=>
                                array(0) {
                                }
                                ["delays":"GuzzleHttp\Handler\CurlMultiHandler":private]=>
                                array(0) {
                                }
                              }
                              ["sync"]=>
                              object(GuzzleHttp\Handler\CurlHandler)#333 (1) {
                                ["factory":"GuzzleHttp\Handler\CurlHandler":private]=>
                                object(GuzzleHttp\Handler\CurlFactory)#334 (2) {
                                  ["handles":"GuzzleHttp\Handler\CurlFactory":private]=>
                                  array(1) {
                                    [0]=>
                                    resource(276) of type (curl)
                                  }
                                  ["maxHandles":"GuzzleHttp\Handler\CurlFactory":private]=>
                                  int(3)
                                }
                              }
                            }
                            ["parameter"]=>
                            array(2) {
                              ["$request"]=>
                              string(10) "<required>"
                              ["$options"]=>
                              string(10) "<required>"
                            }
                          }
                          ["streaming"]=>
                          object(GuzzleHttp\Handler\StreamHandler)#336 (1) {
                            ["lastHeaders":"GuzzleHttp\Handler\StreamHandler":private]=>
                            array(0) {
                            }
                          }
                        }
                        ["parameter"]=>
                        array(2) {
                          ["$request"]=>
                          string(10) "<required>"
                          ["$options"]=>
                          string(10) "<required>"
                        }
                      }
                    }
                  }
                  ["parameter"]=>
                  array(2) {
                    ["$request"]=>
                    string(10) "<required>"
                    ["$options"]=>
                    string(10) "<required>"
                  }
                }
              }
            }
            ["parameter"]=>
            array(2) {
              ["$request"]=>
              string(10) "<required>"
              ["$options"]=>
              string(10) "<required>"
            }
          }
        }
        ["allow_redirects"]=>
        array(5) {
          ["max"]=>
          int(5)
          ["protocols"]=>
          array(2) {
            [0]=>
            string(4) "http"
            [1]=>
            string(5) "https"
          }
          ["strict"]=>
          bool(false)
          ["referer"]=>
          bool(false)
          ["track_redirects"]=>
          bool(false)
        }
        ["http_errors"]=>
        bool(true)
        ["decode_content"]=>
        bool(true)
        ["verify"]=>
        bool(true)
        ["cookies"]=>
        bool(false)
        ["headers"]=>
        array(1) {
          ["User-Agent"]=>
          string(38) "GuzzleHttp/6.3.3 curl/7.52.1 PHP/7.2.8"
        }
      }
    }
    ["paginator":protected]=>
    NULL
    ["token":protected]=>
    NULL
    ["clientId":protected]=>
    string(30) "REDACTED"
    ["clientSecret":protected]=>
    string(30) "REDACTED"
    ["redirectUri":protected]=>
    string(43) "REDACTED"
  }
}
romanzipp commented 5 years ago

You can access the Response payload via the response attribute on the Result class while I have a look in how to fix this permanently.

Twitch::setClientId('REDACTED');
Twitch::setClientSecret('REDACTED');

$result = Twitch::getOAuthToken($code);

$data = json_decode((string) $result->response->getBody());
romanzipp commented 5 years ago

Could you try out version 2.1.1?

I've added fallback support for non-helix standardized response payloads as seen in this test

JasonRivers commented 5 years ago

Thanks! I'll give it a shot this week!