douniang3866 2019-04-04 21:13
浏览 135
已采纳

在PHP中向RESTlet发送GET请求时NetSuite“INVALID_LOGIN错误”

A few months ago I implemented a RESTlet solution that receives a POST request from a PHP script. I am now extending the solution to send a GET request to a RESTlet with a different ID. The issue is that the authentication is only successful when the request is sent to a URL without the query string. We need to pass the query string to the RESTlet in order for it to execute.

Appending the query string to the URL throws a 403 INVALID_LOGIN error, and calling the RESTlet without it throws an exception for a missing paramter (as expected).

private function setAuthentication() {
      $oauthNonce = md5(mt_rand());
      $oauthTimestamp = time();
      $baseString = $this->requestType."&".urlencode($this->url)."&".urlencode(
          "deploy=".$this->deployID
          ."&oauth_consumer_key=".$this->consumerKey
          ."&oauth_nonce=".$oauthNonce
          ."&oauth_signature_method=".$this->oauthSigMethod
          ."&oauth_timestamp=".$oauthTimestamp
          ."&oauth_token=".$this->tokenID
          ."&oauth_version=".$this->oauthVersion
          ."&realm=".$this->account
          ."&script=".$this->scriptID
        );
      $sigString = urlencode($this->consumerSecret).'&'.urlencode($this->tokenSecret);
      $signature = base64_encode(hash_hmac('sha1', $baseString, $sigString, true));
      $authHeader = "OAuth "
        . 'oauth_signature="' . rawurlencode($signature) . '", '
        . 'oauth_version="' . rawurlencode($this->oauthVersion) . '", '
        . 'oauth_nonce="' . rawurlencode($oauthNonce) . '", '
        . 'oauth_signature_method="' . rawurlencode($this->oauthSigMethod) . '", '
        . 'oauth_consumer_key="' . rawurlencode($this->consumerKey) . '", '
        . 'oauth_token="' . rawurlencode($this->tokenID) . '", '  
        . 'oauth_timestamp="' . rawurlencode($oauthTimestamp) . '", '
        . 'realm="' . rawurlencode($this->account) .'"';
        return $authHeader;
    }

public function callGetRestlet() {
      $this->requestType = 'GET';
        $authorizationHeader = $this->setAuthentication();
        $urlQueryAppend = http_build_query(array('id' => $this->queryString));
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->requestType);
      curl_setopt($ch, CURLOPT_URL, $this->url. '?&script='. $this->scriptID. '&deploy='. $this->deployID.'&realm=' . $this->account);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_TIMEOUT, 3);
      curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'Authorization: '.$authorizationHeader,
            'Content-Type: application/json',
      ]);
      $response = array();
      $response['request'] = json_decode($this->queryString);
      $response['response']['body'] = json_decode(curl_exec($ch));
      $response['response']['code']  = curl_getinfo($ch, CURLINFO_HTTP_CODE);
      curl_close($ch);
      return $response;
    }

I know the credentials are okay as the RESTlet's event log shows the exception when the parameter isn't provided. The above throws the exception.

When the CURLOPT_URL is modified to include the querystring it throws the 403:

curl_setopt($ch, CURLOPT_URL, $this->url. '?&script='. $this->scriptID. '&deploy='. $this->deployID.'&realm=' . $this->account.'&'.$urlQueryAppend);

This code was slightly modified from an answer to a different question that asked about a POST request.

Is the error being thrown as a result of setting the request headers?

  • 写回答

1条回答 默认 最新

  • dronthpi05943 2019-04-08 20:31
    关注

    Adding the id parameter to the base string after the deploy ID resolved the issue. I was adding it to the end, but in this case it looks like the order of the parameters matters.

    private function setAuthentication() {
       $oauthNonce = md5(mt_rand());
       $oauthTimestamp = time();
       $baseString = $this->requestType."&".urlencode($this->url)."&".urlencode(
            "deploy=".$this->deployID
            ."&id=".$this->queryString
            ."&oauth_consumer_key=".$this->consumerKey
            ."&oauth_nonce=".$oauthNonce
            ."&oauth_signature_method=".$this->oauthSigMethod
            ."&oauth_timestamp=".$oauthTimestamp
            ."&oauth_token=".$this->tokenID
            ."&oauth_version=".$this->oauthVersion
            ."&script=".$this->scriptID
          );
       $sigString = urlencode($this->consumerSecret).'&'.urlencode($this->tokenSecret);
       $signature = base64_encode(hash_hmac('sha1', $baseString, $sigString, true));
       $authHeader = "OAuth "
          . 'oauth_signature="' . rawurlencode($signature) . '", '
          . 'oauth_version="' . rawurlencode($this->oauthVersion) . '", '
          . 'oauth_nonce="' . rawurlencode($oauthNonce) . '", '
          . 'oauth_signature_method="' . rawurlencode($this->oauthSigMethod) . '", '
          . 'oauth_consumer_key="' . rawurlencode($this->consumerKey) . '", '
          . 'oauth_token="' . rawurlencode($this->tokenID) . '", '  
          . 'oauth_timestamp="' . rawurlencode($oauthTimestamp) . '", '
          . 'realm="' . rawurlencode($this->account) .'"';
         return $authHeader;
    }
    
    public function callGetRestlet() {
          $this->requestType = 'GET';
          $parameters = http_build_query(
            array(
              'script' => $this->scriptID,
              'deploy' => $this->deployID,
              'id' => $this->queryString,
            )
          );
          $authorizationHeader = $this->setAuthentication();
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->requestType);
          curl_setopt($ch, CURLOPT_URL, $this->url. '?'.$parameters);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          curl_setopt($ch, CURLOPT_TIMEOUT, 6);
          curl_setopt($ch, CURLOPT_HTTPHEADER, [
                'Authorization: '.$authorizationHeader,
                'Content-Type: application/json',
          ]);
          $response = array();
          $response['request'] = $this->url. '?'.$parameters;
          $response['response']['body'] = json_decode(curl_exec($ch));
          $response['response']['code']  = curl_getinfo($ch, CURLINFO_HTTP_CODE);
          curl_close($ch);
          return $response;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!