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

4

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

查询服务的首选机制是通过SOAP进行的。但是,SOAP有自己的“auth token”概念,该概念设置在XML的正文中。根据Google的文档,我没有旧式的“ClientLogin”令牌可供使用。

我如何使用我的OAuth身份验证访问令牌运行SOAP查询?还是我应该使用不同的机制来查询或进行身份验证?

1个回答

5

我来回答:

通过正常机制进行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

简述:在查询字符串中使用OAuth,从SOAP头中省略所有身份验证信息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接