soap - 使用 OAuth 访问 token 访问 SOAP 服务?

标签 soap oauth google-api

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

查询服务的首选机制是通过 SOAP。然而,SOAP 有它自己的“身份验证 token ”概念,该概念设置在 XML 的主体中。根据 Google 的文档,我没有可使用的旧式“ClientLogin” token 。

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

最佳答案

回答自己:

通过正常机制(即在 JS 中)向 OAuth 进行身份验证:

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 header 是所记录内容的较小版本,省略了仅 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>

body 正常。

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

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 header 中的所有身份验证信息。

关于soap - 使用 OAuth 访问 token 访问 SOAP 服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4734397/

相关文章:

google-api - Google 登录无法使用

javascript - 无法使用 Node.js 服务器连接到 Google Directions API

ios - 升级到 swift 3.0 后,检查街景可用性不起作用。谷歌地图sdk

web-services - 通过 SSL 的 MonoTouch Web 服务请求获取 'authentication or decryption has failed'

c# - Java 客户端的 WCF 服务

facebook - 作为移动设备的 OAuth Facebook 页面(useragent 和 cordova)

android - 在不打开浏览器的情况下使用路标 OAuth 获取 AccessToken(双足 Oauth)

.net - 在 SOAP 调用中查看 xml 请求

c++ - gSoap - 将请求/响应提取为 XML 字符串

r - 搜索推特并通过hashtag获取推文,最大化返回搜索结果数量