【问题标题】:Mule 3.5 - Google OAuth2 ExampleMule 3.5 - 谷歌 OAuth2 示例
【发布时间】:2013-07-29 11:52:56
【问题描述】:

我正在尝试将 Mule 3.5 连接到 Google API(任务、日历等),但在 OAuth2 身份验证方面遇到了各种问题。

谁能给我一个 Mule 项目的示例 .xml 文件,其中包含一个有效的 Google OAuth2 示例(可能还有 Google 的 API 控制台中的设置)。

链接也可以。

【问题讨论】:

    标签: google-api mule google-oauth mule-studio google-api-console


    【解决方案1】:

    您需要使用创建项目按钮在您的 Google 开发者帐户 (https://console.developers.google.com/) 中创建一个应用程序。记下您的项目 ID,您将在 Google 连接器配置中使用它。

    然后您需要点击应用程序并转到APIs & Auth。确保您需要的 API 设置为“开启”状态。在这种情况下,您可能想要打开日历并关闭您不需要的任何其他内容。请注意,大量调用日历服务可能会产生费用或配额限制。

    在 Google 开发者控制台左侧的 APIs & Auth 部分下,您还需要选择凭据。然后点击红色按钮创建新的客户 ID。这将为您提供两个关键信息:

    1. 客户端 ID - 这将进入 Mule 中 Google 连接器中的“consumerKey”
    2. Client Secret - 这进入 Mule 中的“consumerSecret” 连接器

    另一个重要的设置是重定向 URI。这需要类似于:

    http://localhost:8081/oauth2callback
    

    这需要与您放入连接器配置的内容相匹配。如果您在防火墙后面运行 Mule 服务器,则需要配置代理等内容,以便此回调可以到达您的服务器。

    这是我设法开始工作的粗略示例。请务必根据需要替换 clientID clientSecret 和应用程序名称。

    <?xml version="1.0" encoding="UTF-8"?>
    
    <mule xmlns:json="http://www.mulesoft.org/schema/mule/json"
      xmlns:https="http://www.mulesoft.org/schema/mule/https"
          xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking"
      xmlns:objectstore="http://www.mulesoft.org/schema/mule/objectstore"
      xmlns:http="http://www.mulesoft.org/schema/mule/http"
      xmlns:google-calendars="http://www.mulesoft.org/schema/mule/google-calendars"
      xmlns="http://www.mulesoft.org/schema/mule/core"
          xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
      xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.4.1"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans-current.xsd      
              http://www.mulesoft.org/schema/mule/core 
              http://www.mulesoft.org/schema/mule/core/current/mule.xsd
              http://www.mulesoft.org/schema/mule/http 
              http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
              http://www.mulesoft.org/schema/mule/google-calendars 
              http://www.mulesoft.org/schema/mule/google-calendars/1.0/mule-google-calendars.xsd
              http://www.mulesoft.org/schema/mule/objectstore 
              http://www.mulesoft.org/schema/mule/objectstore/1.0/mule-objectstore.xsd
              http://www.mulesoft.org/schema/mule/ee/tracking 
              http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
              http://www.mulesoft.org/schema/mule/https 
              http://www.mulesoft.org/schema/mule/https/current/mule-https.xsd
              http://www.mulesoft.org/schema/mule/json 
              http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
    
    <!-- The 'consumerKey' is Client ID of you google application
         The 'consumerSecret' is the Client Secret of the google application
         The 'applicationName' is the application name you supplied (or Google created for you) when you created your application
         on the google developer console
        -->
    <google-calendars:config-with-oauth
        name="Google_Calendars"
        consumerKey="replace-with-client-ID"
        consumerSecret="replace-with-client-secret" doc:name="Google Calendars"
        applicationName="replace-with-application-name">
    
        <!-- The values here need to match the redirect URL you authorized for your Google Application
             In this case the callback URL would be http://localhost:8081/ouath2callback
         -->
        <google-calendars:oauth-callback-config
            domain="localhost" localPort="8081" path="oauth2callback" remotePort="8081" />
            </google-calendars:config-with-oauth>
    
    
    <!-- This is the objectstore that stores your Auth key which is used in the second flow -->
    <objectstore:config name="ObjectStore" doc:name="ObjectStore" />
    
    <!-- The first flow is executed when you go to http://localhost:8080/oauth-authorize
         It initiates the Google authentication and if successful gets the auth key and puts it into the object store -->
    <flow name="authorizationAndAuthenticationFlow" doc:name="authorizationAndAuthenticationFlow">
        <http:inbound-endpoint exchange-pattern="request-response"
            host="localhost" port="8080" path="oauth-authorize" doc:name="HTTP" />
        <google-calendars:authorize config-ref="Google_Calendars"
            doc:name="Google Calendars" />
        <!-- Your Auth token is store in the key 'accessTokenId' -->    
        <objectstore:store config-ref="ObjectStore" key="accessTokenId"
            value-ref="#[flowVars['OAuthAccessTokenId']]" overwrite="true"
            doc:name="ObjectStore" />
    </flow>
    
    <!-- This flow can be called after the authentication is complete. It uses the previously stored token and to retreive your
         Calendars and return them as JSON -->
    <flow name="getInformationFromCalendar" doc:name="getInformationFromCalendar">
        <http:inbound-endpoint exchange-pattern="request-response"
            host="localhost" port="8081" doc:name="HTTP" />
    
        <!-- The enricher adds the access token to your message -->
        <enricher target="#[flowVars['accessTokenId']]" doc:name="Message Enricher">
            <objectstore:retrieve config-ref="ObjectStore"
                key="accessTokenId" defaultValue-ref="#['']" doc:name="Get AccessToken" />
        </enricher>
        <expression-filter expression="#[flowVars['accessTokenId'] != '']"
            doc:name="Is Access Token Set" />
    
        <!-- gets your first 200 calendars using the accessToken that you enriched the message with-->
        <google-calendars:get-calendar-list
            config-ref="Google_Calendars" maxResults="200"
            pageToken="#[flowVars['GoogleCalendar_NEXT_PAGE_TOKEN']]" doc:name="Google Calendars"
            accessTokenId="#[flowVars['accessTokenId']]" />
        <json:object-to-json-transformer
            doc:name="Object to JSON" />
    </flow>
    
    </mule>
    

    【讨论】:

      【解决方案2】:

      Google Connectors Suite for Mule 有一个完整的例子,包括一个Mule XML configuration

      【讨论】:

      • 看例子,我仍然对消费者密钥和消费者秘密使用什么感到困惑......例如,如果我设置一个oauth服务帐户,公钥指纹是否会是在 Mule Google 连接器配置中用作“消费者密钥”。我将使用什么作为消费者秘密?与公钥指纹关联的 .p12 文件?如果是这样,那将如何输入?
      【解决方案3】:

      我们已经在how to use OAuth connectors 上发布了文档。让我们知道它是否有帮助。

      【讨论】:

      猜你喜欢
      • 2012-06-17
      • 1970-01-01
      • 1970-01-01
      • 2016-03-12
      • 2015-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多