提问者:小点点

如何从api请求shop ify graql-admin-api?


我正试图从我的api请求shop ify graql-admin-api。我是按照图ql-admin-api给的留档做的,但是它还是给我授权错误。


共2个答案

匿名用户

PHP用户可以按照此函数使用GraphQL请求Shopify管理API

我正在使用GuzzleHttp(PHP HTTP客户端)创建请求

public function graph($query , $variables = []){
    $domain = 'xxx.myshopify.com';
    $url = 'https://'.$domain.'/admin/api/2019-10/graphql.json';

    $request = ['query' => $query];

    if(count($variables) > 0) { $request['variables'] = $variables; }

    $req = json_encode($request);
    $parameters['body'] = $req;

    $stack = HandlerStack::create();
    $client = new \GuzzleHttp\Client([
        'handler'  => $stack,
        'headers'  => [
            'Accept'       => 'application/json',
            'Content-Type' => 'application/json',
            'X-Shopify-Access-Token'=>$this->token // shopify app accessToken
        ],
    ]);

    $response = $client->request('post',$url,$parameters);
    return $body =  json_decode($response->getBody(),true);
 }

  $query = "{ shop { name email } }"; // this is example graphQL query

  $response = graph($query) // call this function 

下面的代码可以帮助您检查此graphQL查询的成本

$calls = $response->extensions->cost;
$apiCallLimitGraph = [
     'left'          => (int) $calls->throttleStatus->currentlyAvailable,
     'made'          => (int) ($calls->throttleStatus->maximumAvailable - $calls->throttleStatus->currentlyAvailable),
     'limit'         => (int) $calls->throttleStatus->maximumAvailable,
     'restoreRate'   => (int) $calls->throttleStatus->restoreRate,
     'requestedCost' => (int) $calls->requestedQueryCost,
     'actualCost'    => (int) $calls->actualQueryCost,
];

匿名用户

转到应用程序-

创建私人应用程序后,您将获得密码,您可以使用该密码作为HTTP请求的令牌,标题为“X-Shopify-Access-token”值:password

  curl -X POST \
  https://{shop}.myshopify.com/admin/api/2021-04/graphql.json \
  -H 'Content-Type: application/graphql' \
  -H 'X-Shopify-Access-Token: {password}' \
  -d '
  {
    products(first: 5) {
      edges {
        node {
          id
          handle
        }
      }
      pageInfo {
        hasNextPage
      }
    }
  }
  '

欲了解更多信息,请访问:https://shopify.dev/docs/admin-api/getting-started#authentication

我在NodeJS中使用的方法是使用包“graphql-request”发出请求和

const mutation = gql`
      mutation createProduct(
        $input: ProductInput!
        $media: [CreateMediaInput!]
      ) {
        productCreate(input: $input, media: $media) {
          userErrors {
            field
            message
          }
          product {
            id
            metafields(first: 1) {
              edges {
                node {
                  id
                }
              }
            }
          }
        }
      }
    `;
     //const input = form your own input 
     const res = await graphQLClient.rawRequest(mutation, input);