0

我找不到 Google 对 Oauth 1.0 的任何有效支持,因为它已被弃用。但由于我想访问 Gmail 数据,我必须使用 Oauth 1.0

因此,我从 2 天以来一直在尝试为我的 Google 应用程序获取 RequestToken,但我收到了无效签名错误。我的基本字符串与谷歌返回的调试字符串匹配。

我试过同时使用 HMAC-SHA1 和 RSA-SHA1 并且我的密钥是正确的。(我什至尝试过将令牌和秘密都用作匿名,谷歌建议非注册应用程序使用。)这可能是由于错误的随机数或某些百分比编码问题造成的。同样的方法也适用于 Twitter。

我也在下面粘贴我的代码,如果有人可以帮助或将我指向正确的论坛,我会很高兴。

    $('#GMAIL_BUTTON').click(function () {
    //oauth1 approach similar to twitter
    var requestUrl = "https://www.google.com/accounts/OAuthGetRequestToken";
    var scope = "https://mail.google.com/";
    var clientID = "anonymous";
    var clientSecret = "anonymous";
    var rsakey = "MIICblahblahblah";
    var timestamp = Math.round(new Date().getTime() / 1000.0);
    var nonce = Math.random();        
    var sigBaseStringParams = "oauth_consumer_key=" + clientID;
    sigBaseStringParams += "&" + "oauth_nonce=" + nonce;
    sigBaseStringParams += "&" + "oauth_signature_method=HMAC-SHA1";
    sigBaseStringParams += "&" + "oauth_timestamp=" + timestamp;
    sigBaseStringParams += "&" + "scope=" + encodeURIComponent(scope);
    var sigBaseString = "GET&";
    sigBaseString += encodeURIComponent(requestUrl) + "&" +        encodeURIComponent(sigBaseStringParams) + "\n" + "\n";
    requestUrl += "?scope=https://mail.google.com/";
    var keyText = encodeURIComponent(clientSecret) + "&";
    //var keyText = rsakey;
    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 keyMaterial = Windows.Security.Cryptography.CryptographicBuffer.decodeFromBase64String(rsakey);
    //var macAlgorithmProvider = Windows.Security.Cryptography.Core.AsymmetricKeyAlgorithmProvider.openAlgorithm("RSASIGN_PKCS1_SHA1");
    //var key = macAlgorithmProvider.importKeyPair(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);
    var dataToPost = "OAuth oauth_consumer_key=\"" + clientID + "\", oauth_nonce=\"" + nonce + "\", oauth_signature_method=\"HMAC-SHA1\", oauth_timestamp=\"" + timestamp + "\", oauth_signature=\"" + encodeURIComponent(signature) + "\", oauth_version=\"1.0\"";
    var response = sendGetRequest(requestUrl, dataToPost, null);
});

SendGetRequest 方法是 -

   function sendGetRequest(url, authzheader, params) {
    try {
        var request = new XMLHttpRequest();
        request.open("GET", url, false);
        request.setRequestHeader("Authorization", authzheader);
        request.send(params);
        return request.responseText;
    } catch (err) {
        WinJS.log("Error sending request: " + err, "Web Authentication SDK Sample", "error");
    }
}
4

1 回答 1

0

我在您的代码中找不到任何明显的错误,但您可能会更轻松地在https://developers.google.com/accounts/docs/OAuth2UserAgent使用 Google 的 OAuth 2 客户端应用程序库。此外,您可能想要使用 atom 提要范围 (https://mail.google.com/mail/feed/atom)——您可以在https://code.google.com/尝试使用它和其他范围oauthplayground/ .

于 2012-07-18T09:56:51.267 回答