通过Laravel GuzzleHTTP发出POST请求以进行令牌认证

时间:2019-04-16 08:19:53

标签: laravel guzzlehttp

如何通过Laravel HTTP请求获得令牌认证或sk / ak认证?为此,我拉了第三方软件包GuzzleHTTP。

https://support-intl.huaweicloud.com/api-ecs/en-us_topic_0124306062.html

这是我尝试过的,

public function postToken() {
        $client = new \GuzzleHttp\Client();
        $response = $client->request('POST', url('https://  ecs.af-south-1.myhuaweicloud.com/v3/auth/tokens'), [
            {
                "auth": {
                  "identity": {
                    "methods": [
                      "password"
                    ],
                    "password": {
                      "user": {
                        "name": "username",
                        "password": "mypassword",
                        "domain": {
                          "name": "mydomain"
                        }
                      }
                    }
                  },
                  "scope": {
                    "project": {
                       "name": "projectname" 
                    }
                  }
                }
            }
          ]);
    }

错误是

syntax error, unexpected '{', expecting ']'

2 个答案:

答案 0 :(得分:1)

您的代码中还有一个额外的}

public function postToken() {
        $client = new \GuzzleHttp\Client();
        $response = $client->request('POST', url('https://  ecs.af-south-1.myhuaweicloud.com/v3/auth/tokens'), [
            {
                "auth": {
                  "identity": {
                    "methods": [
                      "password"
                    ],
                    "password": {
                      "user": {
                        "name": "username",
                        "password": "mypassword",
                        "domain": {
                          "name": "mydomain"
                        }
                      }
                    }
                  },
                  "scope": {
                    "project": {
                       "name": "projectname" 
                    }
                  }
                }
            } // HERE IS THE PROBLEM
          ]);
    }

删除它,您可能就准备好了。

这是一个更易于阅读的版本。如果遵循缩进,则更容易发现这种错误。

public function postToken() {
  $client = new \GuzzleHttp\Client();
  $response = $client->request(
    'POST', 
    url('https://  ecs.af-south-1.myhuaweicloud.com/v3/auth/tokens'),
    [
      {
        "auth": {
          "identity": {
            "methods": [
              "password"
            ],
            "password": {
              "user": {
                "name": "username",
                "password": "mypassword",
                "domain": {
                  "name": "mydomain"
                }
              }
            }
          },
        "scope": {
          "project": {
            "name": "projectname" 
          }
        }
      }
    ]
  );
}

答案 1 :(得分:1)

您的请求语法错误。

这样做:

    $response = $client->request('POST', url('https://  ecs.af-south-1.myhuaweicloud.com/v3/auth/tokens'), [
        'json'=>[
            "auth"=> [
                "identity"=> [
                    "methods"=> [
                        "password"
                    ],
                    "password"=> [
                        "user"=> [
                            "name"=> "username",
                            "password"=> "mypassword",
                            "domain"=> [
                                "name"=> "mydomain"
                            ]
                        ]
                    ]
                ],
                "scope"=> [
                    "project"=> [
                        "name"=> "projectname"
                    ]
                ]
            ]
        ]
    ]);