【问题标题】:Using an OAuth access token to access SOAP service?使用 OAuth 访问令牌访问 SOAP 服务?
【发布时间】:2011-01-19 10:55:24
【问题描述】:

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

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

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

【问题讨论】:

    标签: soap oauth google-api


    【解决方案1】:

    回答自己:

    通过正常机制向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)发布此内容,该方法会将所需的 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 标头中的所有身份验证信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-23
      • 1970-01-01
      • 1970-01-01
      • 2012-02-26
      • 2016-12-03
      • 2015-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多