ตัวอย่างลิงก์เนื้อหาดิจิทัลของ Google

โค้ดตัวอย่างมักจะเป็นวิธีที่ง่ายที่สุดในการดูวิธีใช้ API สําหรับลิงก์ไปยังตัวอย่างลิงก์เนื้อหาดิจิทัลของ Google ให้เลือกภาษาโปรแกรมด้านล่าง

ตัวอย่างจะใช้ไลบรารีของไคลเอ็นต์ของ Google API

หากหน้าตัวอย่างของไลบรารียังไม่มีตัวอย่างสําหรับลิงก์เนื้อหาดิจิทัลของ Google คุณยังคงใช้ไลบรารีดังกล่าวได้ และอาจปรับเปลี่ยนตัวอย่างที่ให้ไว้สําหรับ Google API อื่นได้

Python

นี่คือตัวอย่าง Python ที่เรียบง่ายซึ่งแสดงรายการคําสั่งทั้งหมดจากเว็บไซต์หนึ่งๆ แล้วตรวจสอบว่าเว็บไซต์นั้นแสดงคําสั่ง delegate_permission/common.handle_all_urls เกี่ยวกับแอป Android ที่เจาะจงหรือไม่

#!/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

ไม่มีตัวอย่าง Go สําหรับลิงก์เนื้อหาดิจิทัลของ Google เวอร์ชันนี้โดยเฉพาะ

อย่างไรก็ตาม คุณอาจปรับเปลี่ยนตัวอย่าง Go อื่นๆ ได้

Java

ไม่มีตัวอย่าง Java สําหรับลิงก์เนื้อหาดิจิทัลของ Google เวอร์ชันนี้โดยเฉพาะ

คุณอาจปรับเปลี่ยนตัวอย่าง Java อื่นๆ ได้

.NET

ไม่มีตัวอย่าง .NET สําหรับลิงก์เนื้อหาดิจิทัลของ Google เวอร์ชันนี้โดยเฉพาะ

อย่างไรก็ตาม คุณอาจปรับเปลี่ยนตัวอย่าง .NET อื่นๆ ได้

Objective-C

ไม่มีตัวอย่าง Objective-C สําหรับลิงก์เนื้อหาดิจิทัลของ Google เวอร์ชันนี้โดยเฉพาะ

อย่างไรก็ตาม คุณอาจปรับเปลี่ยนตัวอย่างออบเจ็กต์ C อื่นๆ ได้

PHP

ไม่มีตัวอย่าง PHP สําหรับลิงก์เนื้อหาดิจิทัลของ Google เวอร์ชันนี้โดยเฉพาะ

อย่างไรก็ตาม คุณอาจปรับเปลี่ยนตัวอย่าง PHP อื่นใดได้

Ruby

ไม่มีตัวอย่าง Ruby สําหรับลิงก์เนื้อหาดิจิทัลของ Google เวอร์ชันนี้โดยเฉพาะ

อย่างไรก็ตาม คุณอาจปรับเปลี่ยนตัวอย่าง Ruby อื่นๆ ได้