ตัวอย่างลิงก์เนื้อหาดิจิทัลของ 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 Sample อื่นๆ ได้

Java

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

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

.NET

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

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

Objective-C

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

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

PHP

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

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

Ruby

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

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