Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 
oriana_cantarel
Partner - Contributor
Partner - Contributor

Google Api v3 for Work - Is there a way to generate dynamically google license parameters ( license for work) within the qlikview map?

Hi,

I have created a dynamic map using google api v3, I have followed the procedures showed in the link:

http://community.qlik.com/docs/D'OC-5709

- Scatter chart

- Google maps api v3

This map should be used within a company so I need to integrate the google license within the code,  that means insert  clientID  parameter and signature64 parameter  within the code generates the map, like the following url ( it is an example):

https://maps.googleapis.com/maps/api/staticmap?center=%E4%B8%8A%E6%B5%B7+%E4%B8%AD%E5%9C%8B&size=640...

The problem is that the signature64 parameter is unique per URL, so for  any request a new signature must be generated , in fact  users can navigate the map in the dashboard creating new google map requests. Below the google link which describes how use the google api for works:

https://developers.google.com/maps/documentation/business/webservices/auth

In the google site  there are some suggestions which show ways to implement URL signing using server—side code:

  1. 1.       Java

public class UrlSigner {

  // Note: Generally, you should store your private key someplace safe
  // and read them into your code

  private static String keyString = "YOUR_PRIVATE_KEY";

  // The URL shown in these examples must be already
  // URL-encoded. In practice, you will likely have code
  // which assembles your URL from user or web service input
  // and plugs those values into its parameters.
  private static String urlString = "YOUR_URL_TO_SIGN";

  // This variable stores the binary key, which is computed from the string (Base64) key
  private static byte[] key;

  public static void main(String[] args) throws IOException,
    InvalidKeyException, NoSuchAlgorithmException, URISyntaxException {

    // Convert the string to a URL so we can parse it
    URL url = new URL(urlString);

    UrlSigner signer = new UrlSigner(keyString);
    String request = signer.signRequest(url.getPath(),url.getQuery());

    System.out.println("Signed URL :" + url.getProtocol() + "://" + url.getHost() + request);
  }

  public UrlSigner(String keyString) throws IOException {
    // Convert the key from 'web safe' base 64 to binary
keyString = keyString.replace('-', '+');
keyString = keyString.replace('_', '/');
    System.out.println("Key: " + keyString);
    this.key = Base64.decode(keyString);
  }

  public String signRequest(String path, String query) throws NoSuchAlgorithmException,
    InvalidKeyException, UnsupportedEncodingException, URISyntaxException {

    // Retrieve the proper URL components to sign
    String resource = path + '?' + query;

    // Get an HMAC-SHA1 signing key from the raw key bytes
    SecretKeySpec sha1Key = new SecretKeySpec(key, "HmacSHA1");

    // Get an HMAC-SHA1 Mac instance and initialize it with the HMAC-SHA1 key
    Mac mac = Mac.getInstance("HmacSHA1");
    mac.init(sha1Key);

    // compute the binary signature for the request
    byte[] sigBytes = mac.doFinal(resource.getBytes());

    // base 64 encode the binary signature
    String signature = Base64.encodeBytes(sigBytes);

    // convert the signature to 'web safe' base 64
signature = signature.replace('+', '-');
signature = signature.replace('/', '_');

    return resource + "&signature=" + signature;
  }

  }

  1. 2.       Phyton
#!/usr/bin/python
# -*- coding: utf-8 -*-
""" Sign a URL using the client_secret """

import sys
import hashlib
import urllib
import hmac
import base64
import urlparse

def sign_url(input_url=None, client_id=None, client_secret=None😞
""" Sign a request URL with a Crypto Key.

      Usage:
      from urlsigner import sign_url

      signed_url = sign_url(input_url=my_url,
                            client_id=CLIENT_ID,
                            client_secret=CLIENT_SECRET)

      Args:
      input_url - The URL to sign
      client_id - Your Client ID
      client_secret - Your Crypto Key

      Returns:
      The signed request URL
  """


# Return if any parameters aren't given
if not input_url or not client_id or not client_secret:
return None

# Add the Client ID to the URL
  input_url
+= "&client=%s" % (client_id)

  url
= urlparse.urlparse(input_url)

# We only need to sign the path+query part of the string
  url_to_sign
= url.path + "?" + url.query

# Decode the private key into its binary format
# We need to decode the URL-encoded private key
  decoded_key
= base64.urlsafe_b64decode(client_secret)

# Create a signature using the private key and the URL-encoded
# string using HMAC SHA1. This signature will be binary.
  signature
= hmac.new(decoded_key, url_to_sign, hashlib.sha1)

# Encode the binary signature into base64 for use within a URL
  encoded_signature
= base64.urlsafe_b64encode(signature.digest())

  original_url
= url.scheme + "://" + url.netloc + url.path + "?" + url.query

# Return signed URL
return original_url + "&signature=" + encoded_signature

  1. 3.C#
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace SignUrl {

public struct GoogleSignedUrl {

public static string Sign(string url, string keyString) {
ASCIIEncoding encoding = new ASCIIEncoding();

// converting key to bytes will throw an exception, need to replace '-' and '_' characters first.
string usablePrivateKey = keyString.Replace("-", "+").Replace("_", "/");
byte[] privateKeyBytes = Convert.FromBase64String(usablePrivateKey);

Uri uri = new Uri(url);
byte[] encodedPathAndQueryBytes = encoding.GetBytes(uri.LocalPath + uri.Query);

// compute the hash
      HMACSHA1 algorithm
= new HMACSHA1(privateKeyBytes);
byte[] hash = algorithm.ComputeHash(encodedPathAndQueryBytes);

// convert the bytes to string and make url-safe by replacing '+' and '/' characters
string signature = Convert.ToBase64String(hash).Replace("+", "-").Replace("/", "_");

// Add the signature to the existing URI.
return uri.Scheme+"://"+uri.Host+uri.LocalPath + uri.Query +"&signature=" + signature;
}
}

class Program {

static void Main() {

// Note: Generally, you should store your private key someplace safe
// and read them into your code

const string keyString = "YOUR_PRIVATE_KEY";

// The URL shown here is a static URL which should be already
// URL-encoded. In practice, you will likely have code
// which assembles your URL from user or web service input
// and plugs those values into its parameters.
const  string urlString = "YOUR_URL_TO_SIGN";

Console.WriteLine(GoogleSignedUrl.Sign(urlString,keyString));
}
}
}

Is there a way to use these codes inside a qlikview dashboard? Or is there a way to generate the signature parameter inside the dashboard dynamically?

0 Replies