shopwareArchive / AppTemplate

13 stars 8 forks source link

Error while authentification with the Shopware Account Test-Instance #15

Closed robbelDev closed 1 year ago

robbelDev commented 2 years ago

Hi everybody,

when trying to authenticate with the Shopware 6 app test environment to send a request to the Shopware instance, the core code throws an exception. This is thrown by a 302 Http status code:

GuzzleHttp\Psr7\Response^ {#160
  -reasonPhrase: "Found"
  -statusCode: 302
  -headers: array:6 [
    "Content-Length" => array:1 [
      0 => "138"
    ]
    "Content-Type" => array:1 [
      0 => "text/html"
    ]
    "Date" => array:1 [
      0 => "Fri, 19 Aug 2022 13:16:47 GMT"
    ]
    "Location" => array:1 [
      0 => "http://xxx-dxifv.sw-testenvironment.de/shop/public/"
    ]
    "Server" => array:1 [
      0 => "nginx"
    ]
    "Vary" => array:1 [
      0 => "Accept-Encoding"
    ]
  ]
  -headerNames: array:6 [
    "content-length" => "Content-Length"
    "content-type" => "Content-Type"
    "date" => "Date"
    "location" => "Location"
    "server" => "Server"
    "vary" => "Vary"
  ]
  -protocol: "1.1"
  -stream: GuzzleHttp\Psr7\Stream^ {#156
    -stream: stream resource {@242
      wrapper_type: "PHP"
      stream_type: "TEMP"
      mode: "w+b"
      unread_bytes: 0
      seekable: true
      uri: "php://temp"
      options: []
    }
    -size: null
    -seekable: true
    -readable: true
    -writable: true
    -uri: "php://temp"
    -customMetadata: []
  }
}
PHP Warning:  Trying to access array offset on value of type null in /vendor/shopware/app-bundle/src/Client/Credentials.php on line 22
PHP Warning:  Trying to access array offset on value of type null in /vendor/shopware/app-bundle/src/Client/Credentials.php on line 23

TypeError {#163
  #message: "Shopware\AppBundle\Client\Credentials::__construct(): Argument #1 ($tokenType) must be of type string, null given, called in /vendor/shopware/app-bundle/src/Client/Credentials.php on line 23"
  #code: 0
  #file: "./vendor/shopware/app-bundle/src/Client/Credentials.php"
  #line: 9
  trace: {
    ./vendor/shopware/app-bundle/src/Client/Credentials.php:9 { …}
    ./vendor/shopware/app-bundle/src/Client/Credentials.php:23 { …}
    ./vendor/shopware/app-bundle/src/Client/ShopClient.php:94 { …}
    ./vendor/shopware/app-bundle/src/Client/ShopClient.php:70 { …}
    ./vendor/shopware/app-bundle/src/Client/ShopClient.php:32 { …}
    ./src/Helper/ShopFunctions.php:45 {
      App\Helper\ShopFunctions::getCurrencies(ShopClient $client)^
      ›     'api/search/currency',
      ›     body: json_encode([])
      › ));
    }
    ./src/Command/DiscountCommand.php:55 { …}
    ./vendor/symfony/console/Command/Command.php:299 { …}
    ./vendor/symfony/console/Application.php:1006 { …}
    ./vendor/symfony/framework-bundle/Console/Application.php:96 { …}
    ./vendor/symfony/console/Application.php:295 { …}
    ./vendor/symfony/framework-bundle/Console/Application.php:82 { …}
    ./vendor/symfony/console/Application.php:167 { …}
    ./vendor/symfony/runtime/Runner/Symfony/ConsoleApplicationRunner.php:56 { …}
    ./vendor/autoload_runtime.php:35 { …}
    ./bin/console_2:11 { …}
  }

The calling code:

static function getCurrencies(ShopClient $client)
    {
        $currencyResponse = $client->sendRequest(new Request(
            'POST',
            'api/search/currency',
            body: json_encode([])
        ));

        return json_decode($currencyResponse->getBody()->getContents(), true);
}

Thanks for looking at

coditive-marcin-p commented 1 year ago

Hello, I think I have the same issue. I am testing my app in the Shopware account test environment and have one problem. Every API call from the external server to the shopware returns 302 instead 200. This problem DID NOT occur on different servers when I tested my app. Also, I tested these API calls by Postman and everything works but by Postman I am using a bearer token. It looks like that connection by integrations returns 302. Can someone help me with this? or give some idea what can be the problem?

Is there any chance to help us?

fschmtt commented 1 year ago

Thanks for bringing this up again.

I guess the problem is the configuration of the test environments: The shop-url in the registration request does not contain the path /shop/public/.

Could you please verify that the shop-url in your app server contains the full path?

I see two options to solve this (both on Shopware's side):

@Christian-Rades FYI 🙂

fschmtt commented 1 year ago

After receiving some more feedback from @coditive-marcin-p (thanks for that! 💙), the problem was that they used an absolute path /api/system-config (with a leading slash). Since the base_uri of the Guzzle client is configured to have a trailing slash, a relative path (i.e. api/system-config) must be used to solve the issue. This will also work for shops not being served from a subfolder.

I know it's been a while since you opened this issue @robbelDev and in your code snippet, you're already using a relative path. Were you able to find a solution in the meantime or does the issue still persist?

fschmtt commented 1 year ago

@robbelDev I just saw that your base_uri is using http:// instead of https:// - I guess this is the reason for the redirect 😅

Christian-Rades commented 1 year ago

I'll consider this case closed. If the problem persists, feel free to reopen