Question

I'm trying to connect Mule 3.5 to the Google API (Tasks, Calender etc) but I'm having all sorts of problems with the OAuth2 authentication.

Could anybody give me an example .xml file of a Mule project with a working Google OAuth2 Example (and maybe the settings in Google's API Console), please.

A link would do too.

Était-ce utile?

La solution 2

The Google Connectors Suite for Mule has a complete example, including a Mule XML configuration.

Autres conseils

You need to create an application in your Google Developer account (https://console.developers.google.com/) using the create project button. Take note of your project ID you will need this in the Google connector configuration.

You then need to click on the application and go to APIs & Auth. Make sure the API that you need is set to status 'ON'. In this case you probably want to turn Calendar on and anything else you don't need off. Be aware that significant numbers of calls to the Calendar service might incur costs or quota limits.

Also under the APIs & Auth section of the left side of the Google developer console you need to select credentials. Then click the red button Create new client ID. This will give you two critical pieces of information:

  1. Client ID - This goes into your 'consumerKey' in the Google connector in Mule
  2. Client Secret - This goes into your 'consumerSecret' in the Mule connector

The other important thing to set up is the redirect URI. This will need to be something like:

http://localhost:8081/oauth2callback

This needs to match what you put into your connector configuration. If you're running your Mule server behind a firewall you will need to configure things such as your proxy so this callback can reach your server.

Here is a crude example that I've managed to get working. Be sure to replace the clientID clientSecret and application name as appropriate.

<?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>

We've since published documentation on how to use OAuth connectors. Let us know if it's helpful.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top