Uploaded image for project: 'Bitbucket Cloud'
  1. Bitbucket Cloud
  2. BCLOUD-8849

Oauth returns Could not verify OAuth request.

    XMLWordPrintable

Details

    Description

      Hello
      i want to use the OAuth api
      now the problem is https://bitbucket.org/!api/1.0/oauth/access_token returns evers time "Could not verify OAuth request."

      here is my script

      #!php
      <?php
      try {
      	// fetch access_token
      	$oauthHeader = array(
      		&#x27;oauth_consumer_key&#x27; => StringUtil::trim(BITBUCKET_PUBLIC_KEY),
      		&#x27;oauth_nonce&#x27; => StringUtil::getRandomID(),
      		&#x27;oauth_signature_method&#x27; => &#x27;HMAC-SHA1&#x27;,
      		&#x27;oauth_timestamp&#x27; => TIME_NOW,
      		&#x27;oauth_version&#x27; => &#x27;1.0&#x27;,
      		&#x27;oauth_token&#x27; => $_GET[&#x27;oauth_token&#x27;]
      	);
      	$postData = array(
      		&#x27;oauth_verifier&#x27; => $_GET[&#x27;oauth_verifier&#x27;]
      	);
      	
      	$signature = $this->createSignature(&#x27;https://bitbucket.org/!api/1.0/oauth/access_token&#x27;, array_merge($oauthHeader, $postData));
      	$oauthHeader[&#x27;oauth_signature&#x27;] = $signature;
      	
      	$request = new HTTPRequest(&#x27;https://bitbucket.org/!api/1.0/oauth/access_token&#x27;, array(), $postData);
      	$request->addHeader(&#x27;Authorization&#x27;, &#x27;OAuth &#x27;.$this->buildOAuthHeader($oauthHeader));
      	$request->execute();
      	$reply = $request->getReply();
      	$content = $reply[&#x27;body&#x27;];
      }
      catch (SystemException $e) {
      	throw new IllegalLinkException();
      }
      
      public function buildOAuthHeader(array $parameters) {
      	$header = &#x27;&#x27;;
      	foreach ($parameters as $key => $val) {
      		if ($header !== &#x27;&#x27;) $header .= &#x27;, &#x27;;
      		$header .= rawurlencode($key).&#x27;="&#x27;.rawurlencode($val).&#x27;"&#x27;;
      	}
      	
      	return $header;
      }
      
      public function createSignature($url, array $parameters, $tokenSecret = &#x27;&#x27;) {
      	$tmp = array();
      	foreach ($parameters as $key => $val) {
      		$tmp[rawurlencode($key)] = rawurlencode($val);
      	}
      	$parameters = $tmp;
      	
      	uksort($parameters, &#x27;strcmp&#x27;);
      	$parameterString = &#x27;&#x27;;
      	foreach ($parameters as $key => $val) {
      		if ($parameterString !== &#x27;&#x27;) $parameterString .= &#x27;&&#x27;;
      		$parameterString .= $key.&#x27;=&#x27;.$val;
      	}
      	
      	$base = "POST&".rawurlencode($url)."&".rawurlencode($parameterString);
      	$key = rawurlencode(StringUtil::trim(BITBUCKET_PRIVATE_KEY)).&#x27;&&#x27;.rawurlencode($tokenSecret);
      	
      	return base64_encode(hash_hmac(&#x27;sha1&#x27;, $base, $key, true));
      }
      
      ?>
      

      can anybody say me whats wrong?

      Attachments

        Activity

          People

            Unassigned Unassigned
            1abffdc8c077 Stricted
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: