UpgradeableApp API无效的签名错误

时间:2014-08-26 13:48:20

标签: google-oauth google-apps google-apps-marketplace

我正在尝试运行一个代码,允许我迁移Google Apps Marketplace以升级我的应用,以便对订阅的域使用OAuth 2.0身份验证。 不幸的是,我收到以下错误消息:

{"error":{"errors":[{"domain":"global","reason":"authError","message":"Invalid OAuth signature","locationType":"header","location":"Authorization"}],"code":401,"message":"Invalid OAuth signature"}}

我运行的代码是:

    final String consumerKey = "xxx";
    final String consumerSecret = "yyy";

    final String domain = "example.com";
    final String account = "admin@example.com";

    OAuthParameters oauthParameters = new OAuthParameters();
    oauthParameters.setOAuthConsumerKey(consumerKey); 
    oauthParameters.setOAuthConsumerSecret(consumerSecret);

    OAuthHmacSha1Signer signer = new OAuthHmacSha1Signer();

    final String marketPlaceListingId = "xxxx+xxxxxxxxxxxxxxxxxxx";
    final String chromeWebStoreItemId = "yyyyyyyyyyyyyyyyyyyyyyyyyyyy";

    String requestUrl = "https://www.googleapis.com/appsmarket/v2/upgradableApp/" 
                            + marketPlaceListingId + "/" + chromeWebStoreItemId + "/" + domain;
    String oauth_nonce = OAuthUtil.getNonce();
    String oauth_timestamp = OAuthUtil.getTimestamp().toString();
    String signatureMethod = "HMAC-SHA1";

    oauthParameters.setOAuthType(OAuthType.TWO_LEGGED_OAUTH);
    oauthParameters.setOAuthNonce(oauth_nonce);
    oauthParameters.setOAuthTimestamp(oauth_timestamp);
    oauthParameters.setOAuthSignatureMethod(signatureMethod);

    String baseString = "PUT"
            + "&" + OAuthUtil.encode(requestUrl)
            + "&" + OAuthUtil.encode("oauth_consumer_key=" + oauthParameters.getOAuthConsumerKey()
            + "&oauth_nonce=" + oauth_nonce
            + "&oauth_signature_method=" + signatureMethod
            + "&oauth_timestamp=" + oauth_timestamp
            + "&oauth_version=1.0"
            + "&xoauth_requestor_id=" + OAuthUtil.encode(account));

    String oauth_signature = signer.getSignature(baseString, oauthParameters);

    oauthParameters.setOAuthSignature(oauth_signature);

    OAuthHelper oauthHelper = new GoogleOAuthHelper(signer);
    String header = oauthHelper.getAuthorizationHeader(requestUrl + "?xoauth_requestor_id=" + OAuthUtil.encode(account), "PUT", oauthParameters); 

    HttpClient httpClient = new DefaultHttpClient();

    HttpPut httpPut = new HttpPut(requestUrl + "?xoauth_requestor_id=" + OAuthUtil.encode(account));
    httpPut.addHeader("Authorization", header);
    //httpPut.addHeader("Content-Type", "application/x-www-form-urlencoded");
    HttpResponse response = null;
    try {
        response = httpClient.execute(httpPut);
    } catch (Exception e) {
        System.out.println(e.getMessage());
        throw new ServletException(e);
    }

请支持。

谢谢你, 叶夫

0 个答案:

没有答案