Google Digital Asset Links 示例

示例代码通常是学习如何使用 API 的最简单方法。对于 Google Digital Asset Links 示例的链接,请在下方选择编程语言。

这些示例使用 Google API 客户端库

如果某个库的示例页面尚未包含 Google Digital Asset Links 示例,您仍然可以使用该库,或许可以调整为其他 Google API 提供的示例。

Python

下面是一个简单的 Python 示例,其中列出了给定网站做出的所有声明,然后检查该网站是否针对某个特定 Android 应用创建了 delegate_permission/common.handle_all_urls 语句。

#!/usr/bin/python

import urllib

def ListWeb(source_web_site, relation):
  return urllib.urlopen(
      'https://digitalassetlinks.googleapis.com/v1/'
      'statements:list?source.web.site=%s&relation=%s'
      % (urllib.quote(source_web_site, ''),
         urllib.quote(relation, ''))).read()

def CheckWebToAndroid(source_web_site, relation,
                      target_package_name, target_sha256_fingerprint):
  return urllib.urlopen(
      'https://digitalassetlinks.googleapis.com/v1/'
      'assetlinks:check?source.web.site=%s&relation=%s'
      '&target.android_app.package_name=%s'
      '&target.android_app.certificate.sha256_fingerprint=%s'
      '&key=API_KEY'
      % (urllib.quote(source_web_site, ''),
         urllib.quote(relation, ''),
         urllib.quote(target_package_name, ''),
         urllib.quote(target_sha256_fingerprint, ''))).read()

def main():
  print '================================== List() Output ======='
  print ListWeb('http://example.digitalassetlinks.org',
                'delegate_permission/common.handle_all_urls')
  print '================================== Check() Output ======'
  print CheckWebToAndroid(
      'http://example.digitalassetlinks.org',
      'delegate_permission/common.handle_all_urls',
      'org.digitalassetlinks.sampleapp',
      '10:39:38:EE:45:37:E5:9E:8E:E7:92:F6:54:50:4F:B8:34:6F:C6:B3:46:D0:BB:C4:41:5F:C3:39:FC:FC:8E:C1')

if __name__ == '__main__':
  main()

JavaScript

下面是一个简单的 JavaScript 示例,可让您列出给定网站的所有语句,并检查该网站中是否存在给定语句。

<html>
  <head>
    <script type="text/javascript">
      function executeRequest(request, outElement) {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
          if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200) {
              outElement.value = xmlhttp.responseText;
            } else {
              outElement.value = "Error running request. Response: "
                  + xmlhttp.responseText;
            }
          }
        };
        xmlhttp.open('GET', 'https://digitalassetlinks.googleapis.com/v1/' +
            request, true);
        xmlhttp.send();
      }

      function executeListRequest() {
        var sourceWebSite = encodeURIComponent(
            document.getElementById('list_source').value);
        var relation = encodeURIComponent(
            document.getElementById('list_relation').value);
        var outputTextArea = document.getElementById('list_response');
        executeRequest('statements:list?source.web.site=' + sourceWebSite
            + '&relation=' + relation, outputTextArea);
      }

      function executeCheckRequest() {
        var sourceWebSite = encodeURIComponent(
            document.getElementById('check_source').value);
        var relation = encodeURIComponent(
            document.getElementById('check_relation').value);
        var targetPackageName = encodeURIComponent(
            document.getElementById('check_target_package').value);
        var targetSha256Fingerprint = encodeURIComponent(
            document.getElementById('check_target_sha256_fingerprint').value);
        var outputTextArea = document.getElementById('check_response');
        executeRequest('assetlinks:check?source.web.site=' + sourceWebSite
            + '&relation=' + relation
            + '&target.android_app.package_name=' + targetPackageName
            + '&target.android_app.certificate.sha256_fingerprint='
            +     targetSha256Fingerprint
            + '&key=API_KEY',
            outputTextArea);
      }

    </script>
  </head>
  <body>
    <h2>List()</h2>
    <label>Source Web Asset:</label>
    <input type="text" id="list_source"
        value="http://example.digitalassetlinks.org">
     
    <label>Relation:</label>
    <input type="text" id="list_relation"
        value="delegate_permission/common.handle_all_urls">
     
    <button type="button" onclick="executeListRequest()">Run</button><br>
    <textarea rows="20" cols="80" id="list_response"></textarea>
    <hr>
    <h2>Check()</h2>
    <label>Source Web Asset:</label>
    <input type="text" id="check_source"
        value="http://example.digitalassetlinks.org">
     
    Relation:
    <input type="text" id="check_relation"
        value="delegate_permission/common.handle_all_urls"><br>
     
    <label>Target Android Package:</label>
    <input type="text" id="check_target_package"
        value="org.digitalassetlinks.sampleapp">
     
    <label>Target Android Certificate Fingerprint:</label>
    <input type="text" id="check_target_sha256_fingerprint"
        value="10:39:38:EE:45:37:E5:9E:8E:E7:92:F6:54:50:4F:B8:34:6F:C6:B3:46:D0:BB:C4:41:5F:C3:39:FC:FC:8E:C1">
     
    <button type="button" onclick="executeCheckRequest()">Run</button><br>
    <textarea rows="20" cols="80" id="check_response"></textarea>
  </body>
</html>

Go

此版本的 Google Digital Asset Links 没有专门的 Go 示例。

但是,您可以修改其他某个 Go 示例

Java

此版本的 Google Digital Asset Links 没有专门的 Java 示例。

您可以修改其他某个 Java 示例

.NET

此版本的 Google Digital Asset Links 没有专门的 .NET 示例。

但是,您可以修改其他某个 .NET 示例

Objective-C

此版本的 Google Digital Asset Links 没有专门针对 Objective-C 示例。

但是,您可以修改其他某个 Objective-C 示例

PHP

此版本的 Google Digital Asset Links 没有专门的 PHP 示例。

但是,您可以修改其他某个 PHP 示例

Ruby

此版本的 Google Digital Asset Links 没有专门针对 Ruby 的示例。

但是,您可以修改其他某个 Ruby 示例