2012-07-20 75 views
0

我試圖獲取通過谷歌的UserInfo API「https://sites.google.com/site/oauthgoog/Home/emaildisplayscope」憑證無效的錯誤 - 谷歌用戶信息API

用戶的電子郵件地址我正在使用有效的訪問令牌,但仍然收到無效的錯誤。 我的應用程序正在實施Oauth 1.0是否有可能API需要Oauth 2令牌。 如果是,那麼獲取用戶電子郵件地址的另一種方式是什麼?

這裏是我的代碼 -

 $('#GMAIL_BUTTON').click(function() { 
    //oauth1 approach similar to twitter 
    var requestUrl = "https://www.google.com/accounts/OAuthGetRequestToken"; 
    var authorizeUrl = "https://www.google.com/accounts/OAuthAuthorizeToken"; 
    var accessUrl = "https://www.google.com/accounts/OAuthGetAccessToken"; 
    var callbackUrl = "http://abcd.com/dfdf.php"; 
    var scope = "https://mail.google.com/ https://www.googleapis.com/auth/userinfo.email  https://www.googleapis.com/auth/userinfo.profile"; 
    var clientID = "vfdvfdvfd"; 
    var clientSecret = "dvdfvrevvfv-fvfdvf"; 
    var timestamp = Math.round(new Date().getTime()/1000.0); 
    var nonce = (new Date()).getTime(); 
    var params = []; 
    params["oauth_callback"] = encodeURI(callbackUrl); 
    params["oauth_consumer_key"] = clientID; 
    params["oauth_timestamp"] = timestamp; 
    params["oauth_nonce"] = nonce; 
    params["oauth_signature_method"] = "HMAC-SHA1"; 
    params["scope"] = scope; 
    var paramString = normalizeParams(params); 
    var sigBaseString = "GET&" + encodeURIComponent(requestUrl) + "&" + encodeURIComponent(paramString); 
    var keyText = encodeURIComponent(clientSecret) + "&"; 
    var keyMaterial = Windows.Security.Cryptography.CryptographicBuffer.convertStringToBinary(keyText, Windows.Security.Cryptography.BinaryStringEncoding.Utf8); 
    var macAlgorithmProvider = Windows.Security.Cryptography.Core.MacAlgorithmProvider.openAlgorithm("HMAC_SHA1"); 
    var key = macAlgorithmProvider.createKey(keyMaterial); 
    var tbs = Windows.Security.Cryptography.CryptographicBuffer.convertStringToBinary(sigBaseString, Windows.Security.Cryptography.BinaryStringEncoding.Utf8); 
    var signatureBuffer = Windows.Security.Cryptography.Core.CryptographicEngine.sign(key, tbs); 
    var signature = Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(signatureBuffer); 
    paramString += "&oauth_signature=" + encodeURIComponent(signature); 
    requestUrl = encodeURI(requestUrl); 
    requestUrl += "?" + paramString; 
    var response = sendGetRequest(requestUrl); 
    //requestUrl += "?scope="+encodeURIComponent(scope); 
    //var response = sendGetRequest(requestUrl, dataToPost, null); 
    var keyValPairs = response.split("&"); 
    var oauth_token; 
    var oauth_token_secret; 
    for (var i = 0; i < keyValPairs.length; i++) { 
     var splits = keyValPairs[i].split("="); 
     switch (splits[0]) { 
      case "oauth_token": 
       oauth_token = splits[1]; 
       break; 
      case "oauth_token_secret": 
       oauth_token_secret = splits[1]; 
       break; 
     } 
    } 

    // Send the user to authorization 
    authorizeUrl += "?oauth_token=" + oauth_token; 

    // document.getElementById("TwitterDebugArea").value += "\r\nNavigating to: " + twitterURL + "\r\n"; 
    var startURI = new Windows.Foundation.Uri(authorizeUrl); 
    var endURI = new Windows.Foundation.Uri(callbackUrl); 

    //authzInProgress = true; 
    Windows.Security.Authentication.Web.WebAuthenticationBroker.authenticateAsync(
     Windows.Security.Authentication.Web.WebAuthenticationOptions.none, startURI, endURI) 
     .done(function (result) { 
      var value = result.responseData; 
      var callbackPrefix = callbackUrl + "?"; 
      var dataPart = value.substring(callbackPrefix.length); 
      var keyValPairs = dataPart.split("&"); 
      var authorize_token; 
      var oauth_verifier; 
      for (var i = 0; i < keyValPairs.length; i++) { 
       var splits = keyValPairs[i].split("="); 
       switch (splits[0]) { 
        case "oauth_token": 
         authorize_token = splits[1]; 
         break; 
        case "oauth_verifier": 
         oauth_verifier = splits[1]; 
         break; 
       } 
      } 
      if (result.responseStatus === Windows.Security.Authentication.Web.WebAuthenticationStatus.errorHttp) { 
       //document.getElementById("FacebookDebugArea").value += "Error returned: " + result.responseErrorDetail + "\r\n"; 
      } 
      //form the header and send the verifier in the request to accesstokenurl 
      var params = []; 
      var timestamp = Math.round(new Date().getTime()/1000.0); 
      var nonce = (new Date()).getTime(); 
      params["oauth_consumer_key"] = clientID; 
      params["oauth_nonce"] = nonce; 
      params["oauth_signature_method"] = "HMAC-SHA1"; 
      params["oauth_timestamp"] = timestamp; 
      params["oauth_token"] = authorize_token; 
      params["oauth_verifier"] = oauth_verifier;    
      var paramString = normalizeParams(params); 

      var sigBaseString = "GET&" + rfcEncoding(accessUrl) + "&" + rfcEncoding(paramString); 
      var keyText = rfcEncoding(clientSecret) + "&" + rfcEncoding(oauth_token_secret); 
      var keyMaterial = Windows.Security.Cryptography.CryptographicBuffer.convertStringToBinary(keyText, Windows.Security.Cryptography.BinaryStringEncoding.Utf8); 
      var macAlgorithmProvider = Windows.Security.Cryptography.Core.MacAlgorithmProvider.openAlgorithm("HMAC_SHA1"); 
      var key = macAlgorithmProvider.createKey(keyMaterial); 
      var tbs = Windows.Security.Cryptography.CryptographicBuffer.convertStringToBinary(sigBaseString, Windows.Security.Cryptography.BinaryStringEncoding.Utf8); 
      var signatureBuffer = Windows.Security.Cryptography.Core.CryptographicEngine.sign(key, tbs); 
      var signature = Windows.Security.Cryptography.CryptographicBuffer.encodeToBase64String(signatureBuffer); 
      paramString += "&oauth_signature=" + rfcEncoding(signature); 
      accessUrl = encodeURI(accessUrl); 
      accessUrl += "?" + paramString; 
      var response = sendGetRequest(accessUrl); 

      var tokenstartpos = response.indexOf("oauth_token") + 12; 
      var tokenendpos = response.indexOf("&oauth_token_secret"); 
      var secretstartpos = tokenendpos + 20; 
      var token = response.substring(tokenstartpos, tokenendpos); 
      var secret = response.substring(secretstartpos); 


      var gmailinfourl = "https://www.googleapis.com/userinfo/email?access_token="+token; 

      WinJS.xhr({url: gmailinfourl}).done(function success(result) { 
       var gmail_id = JSON.parse(result.responseText).email_address; 

        //send data to server 
        //more code 

      }); 
      /* 

      */ 
     }, function (err) { 
      WinJS.log("Error returned by WebAuth broker: " + err, "Web Authentication SDK Sample", "error"); 
     }); 
});  

回答

1

正確userinfo email scope是這樣的:

https://www.googleapis.com/auth/userinfo.email 

谷歌棄用的OAuth 1 access和鼓勵你升級到OAuth 2(這是更多的方式容易!) ,儘管它應該還能工作一段時間。 然而,如果你註冊了一個新的 client id因爲棄用,這可能是因爲它只與OAuth的工作2.

UPDATE:

你有問題,是您嘗試請求資源在OAuth的2路:

GET /userinfo/email?access_token=... 

的OAuth 1的工作不同,因爲你可以看到Google's OAuth 1 Playground

GET /userinfo/email?oauth_consumer_key={CONSUMER_KEY} 
        &oauth_nonce={NONCE} 
        &oauth_signature={SIGNATURE} 
        &oauth_signature_method={SIGNATURE_METHOD} 
        &oauth_timestamp={TIMESTAMP} 
        &oauth_token={YOUR_TOKEN} 
        &oauth_version=1.0 

更重要的是,這將是在請求頭中的OAuth簽名:

GET /userinfo/email 
[...] 
Authorization: OAuth oauth_consumer_key="{CONSUMER_KEY}", 
        oauth_nonce="{NONCE}", 
        oauth_signature="{SIGNATURE}", 
        oauth_signature_method="{SIGNATURE_METHOD}", 
        oauth_timestamp="{TIMESTAMP}", 
        oauth_token="{YOUR_TOKEN}", 
        oauth_version="1.0" 

header參數如何設置信息。

+0

我沒有選擇,因爲我的應用程序也必須取的界定我使用OAuth 1 Gmail的數據,谷歌不支持的oauth2在IMAP – code4fun 2012-07-21 21:48:46

+0

所以我註冊了我的應用程序是不是意味着deprecation.does後,我可以」 t使用Oauth1令牌。我可以使用它來通過IMAP獲取電子郵件 – code4fun 2012-07-22 03:17:53

+0

因此,您已經有了一個可用的OAuth令牌,但它不適用於其他API而不是Gmail?請提供您發送給Google的獲取令牌的請求流(請記住審查您的憑據)。也許你在做範圍錯誤? – 2012-07-22 09:34:39