Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. signature_invalid in Google API

signature_invalid in Google API

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
helpcomdata-structurescryptographyjson
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Paresh1462
    wrote on last edited by
    #1

    I am developing Google app (with oauth 1.0)that retrieve google contatcs but i got error : signature_invalid please help me Thx in advance

    function GetRequestToken()
    {
    $consumer = 'YOUR_CONSUMER_KEY';
    $secret = 'YOUR_CONSUMER_SECRETE';
    $callback = 'http://localhost/Gcontacts/welcome/redirection';
    $sign_method = 'HMAC-SHA1';
    $version = '1.0';
    $scope = 'https://www.google.com/m8/feeds/';
    $path = "/accounts/OAuthGetRequestToken";
    $mt = microtime();
    $rand = mt_rand();
    $nonce = md5($mt.$rand);
    $time = time();
    $url = 'https://www.google.com/accounts/OAuthGetRequestToken';
    $post = array(
    'oauth_callback' => $callback,
    'oauth_consumer_key' => $consumer,
    'oauth_nonce' => $nonce,
    'oauth_signature_method' => $sign_method,
    'oauth_timestamp' => $time,
    'oauth_version' => $version,
    'scope' => $scope
    );
    $post_string1 = '';
    foreach($post as $key => $value)
    {
    $post_string1 .= $key.'='.($value).'&';
    }
    $post_string1 = trim($post_string1, '&');

    $key\_part = $this->urlencodeRFC3986($secret);
    $key = $key\_part;
    $base\_string = $this->calculateBaseString($scope, "GET", $post);
    echo $base\_string;
    $signature = base64\_encode(hash\_hmac('sha1', $base\_string, $key,true));
    $post\_string1 .= 'oauth\_signature'.'='.urlencode($signature);
    		$post\['oauth\_signature'\] = $signature;
    
    $header\[\] = 'POST'.$path.'?scope='.$scope.' HTTP/1.1';
    $header\[\] = 'Content-Type: application/x-www-form-urlencoded';
    $header\[\] = 'Accept: \*/\*';	
    $header\[\] = $this->calculateHeader($post).', Content-Type: application/x-www-form-urlencoded ,Host: www.google.com';			
    $ch = curl\_init();		
    curl\_setopt($ch, CURLOPT\_POSTFIELDS, $post\_string1);
    curl\_setopt($ch, CURLOPT\_HTTPHEADER, $header);
    curl\_setopt($ch, CURLOPT\_URL, $url);
    curl\_setopt($ch, CURLOPT\_POSTFIELDS, 'scope='.$scope);
    curl\_setopt($ch, CURLOPT\_SSL\_VERIFYPEER,false);
    curl\_setopt($ch, CURLOPT\_POST,1);
    curl\_setopt($ch, CURLOPT\_RETURNTRANSFER, true);
    curl\_setopt($ch, CURLOPT\_FOLLOWLOCATION, true);
    $result = curl\_exec($ch);
    curl\_close($ch);
    

    }

    private static function urlencode_rfc3986($value)
    {
    if(is_array($value)) return array_map(array('Welcome_model', 'urlencode_rfc3986'), $value);
    else
    {
    $search = array('+', ' ', '%7E', '%');
    $replace = array('%20', '%20', '~', '%25');

    		return str\_replace($search, $replace, urlencode($value));
    	}
    }
    

    function calculateBaseString($url, $method, array $parameters)
    {
    $url = (string) $url;

    N 1 Reply Last reply
    0
    • P Paresh1462

      I am developing Google app (with oauth 1.0)that retrieve google contatcs but i got error : signature_invalid please help me Thx in advance

      function GetRequestToken()
      {
      $consumer = 'YOUR_CONSUMER_KEY';
      $secret = 'YOUR_CONSUMER_SECRETE';
      $callback = 'http://localhost/Gcontacts/welcome/redirection';
      $sign_method = 'HMAC-SHA1';
      $version = '1.0';
      $scope = 'https://www.google.com/m8/feeds/';
      $path = "/accounts/OAuthGetRequestToken";
      $mt = microtime();
      $rand = mt_rand();
      $nonce = md5($mt.$rand);
      $time = time();
      $url = 'https://www.google.com/accounts/OAuthGetRequestToken';
      $post = array(
      'oauth_callback' => $callback,
      'oauth_consumer_key' => $consumer,
      'oauth_nonce' => $nonce,
      'oauth_signature_method' => $sign_method,
      'oauth_timestamp' => $time,
      'oauth_version' => $version,
      'scope' => $scope
      );
      $post_string1 = '';
      foreach($post as $key => $value)
      {
      $post_string1 .= $key.'='.($value).'&';
      }
      $post_string1 = trim($post_string1, '&');

      $key\_part = $this->urlencodeRFC3986($secret);
      $key = $key\_part;
      $base\_string = $this->calculateBaseString($scope, "GET", $post);
      echo $base\_string;
      $signature = base64\_encode(hash\_hmac('sha1', $base\_string, $key,true));
      $post\_string1 .= 'oauth\_signature'.'='.urlencode($signature);
      		$post\['oauth\_signature'\] = $signature;
      
      $header\[\] = 'POST'.$path.'?scope='.$scope.' HTTP/1.1';
      $header\[\] = 'Content-Type: application/x-www-form-urlencoded';
      $header\[\] = 'Accept: \*/\*';	
      $header\[\] = $this->calculateHeader($post).', Content-Type: application/x-www-form-urlencoded ,Host: www.google.com';			
      $ch = curl\_init();		
      curl\_setopt($ch, CURLOPT\_POSTFIELDS, $post\_string1);
      curl\_setopt($ch, CURLOPT\_HTTPHEADER, $header);
      curl\_setopt($ch, CURLOPT\_URL, $url);
      curl\_setopt($ch, CURLOPT\_POSTFIELDS, 'scope='.$scope);
      curl\_setopt($ch, CURLOPT\_SSL\_VERIFYPEER,false);
      curl\_setopt($ch, CURLOPT\_POST,1);
      curl\_setopt($ch, CURLOPT\_RETURNTRANSFER, true);
      curl\_setopt($ch, CURLOPT\_FOLLOWLOCATION, true);
      $result = curl\_exec($ch);
      curl\_close($ch);
      

      }

      private static function urlencode_rfc3986($value)
      {
      if(is_array($value)) return array_map(array('Welcome_model', 'urlencode_rfc3986'), $value);
      else
      {
      $search = array('+', ' ', '%7E', '%');
      $replace = array('%20', '%20', '~', '%25');

      		return str\_replace($search, $replace, urlencode($value));
      	}
      }
      

      function calculateBaseString($url, $method, array $parameters)
      {
      $url = (string) $url;

      N Offline
      N Offline
      Niall Barr
      wrote on last edited by
      #2

      You need to be very careful while doing this sort of thing - any little errors are going to break it, and can be hard to track down. A few I've spotted are:

      • You have the 'scope' parameter out of sequence when building the data to sign. Data must be ordered alphabetically by key for the oauth signature to be correct.
      • You use "GET" as a parameter for calculateBaseString although you're using POST
      • You miss out the & delimiter when adding the oauth_signature
      • You call curl_setopt($ch, CURLOPT_POSTFIELDS twice, overwriting the OAuth signed data with a single parameter.

      It's very possible there are other errors I've not spotted. Niall

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups