使用OAuth访问令牌访问SOAP服务?

时间:2011-01-19 10:55:25

标签: soap oauth google-api

我目前正在尝试通过Chrome扩展程序访问Google服务。我的理解是,对于JS应用程序,Google首选的身份验证机制是OAuth。我的应用目前通过OAuth成功验证了该服务。

查询服务的首选机制是通过SOAP。但是,SOAP拥有自己的“身份验证令牌”概念,该概念在XML正文中设置。我没有按照Google的文档使用旧式“ClientLogin”令牌。

如何使用经过OAuth验证的访问令牌运行SOAP查询?或者我应该使用不同的机制来查询或验证?

1 个答案:

答案 0 :(得分:4)

回答自己:

通过常规机制对OAuth进行身份验证,即在JS:

var oauth = ChromeExOAuth.initBackgroundPage({
 'request_url': 'https://www.google.com/accounts/OAuthGetRequestToken',
 'authorize_url': 'https://www.google.com/accounts/OAuthAuthorizeToken',
 'access_url': 'https://www.google.com/accounts/OAuthGetAccessToken',
 'consumer_key': 'anonymous',
 'consumer_secret': 'anonymous',
 'scope': 'https://domain_for_your_api/',
 'app_name': 'Your app name'
});

然后验证并运行您的SOAP请求作为回调:

function authenticateAndGetAlerts() {
    oauth.authorize(runMyRequest);
}

SOAP标头是文档的较小版本,省略了ClientLogin API唯一必需的字段。

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="https://adwords.google.com/api/adwords/mcm/v201008" xmlns:ns2="https://adwords.google.com/api/adwords/cm/v201008" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <SOAP-ENV:Header>
  <ns1:RequestHeader xsi:type="ns2:RequestHeader">
   <ns2:developerToken>Your developer token</ns2:developerToken>
   <ns2:userAgent>Your app name</ns2:userAgent>
  </ns1:RequestHeader>
 </SOAP-ENV:Header>

身体是正常的。

然后使用适当的方法(JS中的oauth.sendSignedRequest)对此进行POST,将所需的OAuth字段添加到查询字符串中:

var request = {
 'method': 'POST',
 'body': soapenvelope
};   
oauth.sendSignedRequest(url, callback, request);

完成。如果您需要手动执行查询而不是像sendSignedRequest那样使用某些查询,则它看起来像:

servicename.google.com/where/your/service/lives?oauth_consumer_key=anonymous&oauth_nonce=something&oauth_signature=something&oauth_signature_method=HMAC-SHA1&oauth_timestamp=something&oauth_token=something

TLDR:查询字符串中的OAuth,省略SOAP头中的所有身份验证信息。

相关问题