<ConnectionManager Name="DoubleClick" RuntimeVersion="" Help="http://www.cozyroc.com/ssis/doubleclick-connection" xmlns="http://www.cozyroc.com/schema/rcm-config-1.0.xsd">
	<Service EndPoint="https://www.googleapis.com/dfareporting/v3.4" />

	<Authentication Name="Google OAuth2">
		<Documentation>https://developers.google.com/doubleclick-advertisers/authorizing</Documentation>

		<User>
			<Parameter Name="Scope" Default="https://www.googleapis.com/auth/dfatrafficking https://www.googleapis.com/auth/dfareporting https://www.googleapis.com/auth/ddmconversions" />
		</User>

		<Test Url="/userprofiles" />

		<Token Url="{{=OAuth2.sign_in_url}}" Result="{{=response.access_token}}">
			<Parameters>
				<Parameter Name="response_type" Value="{{=OAuth2.sign_in_response_type}}" />
				<Parameter Name="client_id" Value="{{=OAuth2.client_id}}" />
				<Parameter Name="redirect_uri" Value="{{=OAuth2.redirect_uri}}" />
				<Parameter Name="scope" Value="{{=token.Scope}}" />
				<Parameter Name="access_type" Value="offline" />
			</Parameters>

			<Authorized Id="{{=OAuth2.redirect_uri}}" Result="{{=
				connection.execute({
					url: OAuth2.authorization_url,
					method: OAuth2.authorization_method,
					parameters: {
						code: response.code,
						grant_type: OAuth2.grant_type,
						client_id: OAuth2.client_id,
						client_secret: OAuth2.client_secret,
						redirect_uri: OAuth2.redirect_uri,
						scope: token.Scope
			} })}}">

				<Documentation>https://developers.google.com/identity/protocols/OAuth2WebServer</Documentation>
			</Authorized>

			<Refresh Url="{{=OAuth2.refresh_token_url}}" Method="POST" Result="{{=response.refresh_token}}" Expiration="{{=Date.now() + (response.expires_in - 300) * 1000}}">
				<Documentation>https://developers.google.com/identity/protocols/OAuth2WebServer#refresh</Documentation>

				<Parameters>
					<Parameter Name="refresh_token" Value="{{=token.Refresh}}" />
					<Parameter Name="client_id" Value="{{=OAuth2.client_id}}" />
					<Parameter Name="client_secret" Value="{{=OAuth2.client_secret}}" />
					<Parameter Name="grant_type" Value="refresh_token" />
				</Parameters>
			</Refresh>
		</Token>

		<Parameters>
			<Parameter Name="Authorization" Value="Bearer {{=token.Access}}" Type="HttpHeader" />
		</Parameters>
	</Authentication>

	<Resources>
		<Template>
			<Field Name="ShortText" DataType="DT_WSTR" Length="255" />
			<Field Name="LongText" DataType="DT_WSTR" Length="1000" />
			<Field Name="DateTime" DataType="DT_DBTIMESTAMP" />
			<Field Name="Date" DataType="DT_DBDATE" />

			<Resource Name="Base">
				<Read Result="{{=response}}">
					<Parameters>
						<Parameter Name="pageToken" Value="{{=parameters.iterator}}" />
						<Parameter Name="maxResults" Value="{{=parameters.batchSize}}" />
						<Parameter Name="_includeUserParameters" Value="{{=parameters}}" />
					</Parameters>

					<Iterator>
						<Next Value="{{=response.nextPageToken}}" />
					</Iterator>
				</Read>

				<Create Method="POST">
					<Parameters>
						<Parameter Name="_includeUserParameters" Value="{{=parameters}}" />
						<Parameter Name="application/json" Value="{{=item}}" Type="Body" />
					</Parameters>
				</Create>

				<Update Method="PUT">
					<Parameters>
						<Parameter Name="_includeUserParameters" Value="{{=parameters}}" />
						<Parameter Name="application/json" Value="{{=item}}" Type="Body" />
					</Parameters>
				</Update>

				<Delete Method="DELETE" />
			</Resource>
		</Template>
	</Resources>

	<Script>
		<Module Name="Main">
			<![CDATA[
require("underscore");

		]]>
		</Module>
		<Module Name="Google OAuth2">
			<![CDATA[
var OAuth2 = {
	client_id: "822955360543-0k56u5vu2ssvivg9qno2oisml9322esj.apps.googleusercontent.com",
	client_secret: "Em0SYyJ09-NgLvhyT2hPOlfY",
	sign_in_url: "https://accounts.google.com/o/oauth2/auth",
	sign_in_response_type: "code",
	authorization_url: "https://accounts.google.com/o/oauth2/token",
	authorization_method: "POST",
	redirect_uri: "http://localhost",
	grant_type: "authorization_code",
	refresh_token_url: "https://www.googleapis.com/oauth2/v4/token"
};

		]]>
		</Module>
	</Script>
</ConnectionManager>