การใช้ API

เอกสารนี้อธิบายข้อมูลที่จำเป็นในการใช้ Google Civic Information API คุณสามารถดูคำถามที่พบบ่อยเกี่ยวกับ Civic Info API นี้ได้ ซึ่งมีข้อมูลพื้นฐานบางอย่างเกี่ยวกับ API รวมถึงคําถามที่พบบ่อยจากฟอรัมผู้ใช้ นอกจากนี้ คุณยังดูตัวอย่างการค้นหาข้อมูลผู้มีสิทธิเลือกตั้งระหว่างการเลือกตั้งได้ที่ด้านล่าง

ระบุแอปพลิเคชันของคุณให้ Google ทราบ

แอปพลิเคชันต้องระบุตัวตนทุกครั้งที่ส่งคำขอไปยัง Google Civic Information API โดยใส่คีย์ API ไว้ในคำขอแต่ละรายการ

การรับและใช้คีย์ API

วิธีรับคีย์ API

  1. เปิดหน้าข้อมูลเข้าสู่ระบบใน API Console
  2. API นี้รองรับข้อมูลเข้าสู่ระบบ 2 ประเภท สร้างข้อมูลเข้าสู่ระบบที่เหมาะสมกับโปรเจ็กต์ของคุณ ดังนี้
    • OAuth 2.0: เมื่อใดก็ตามที่แอปพลิเคชันขอข้อมูลส่วนตัวของผู้ใช้ แอปพลิเคชันจะต้องส่งโทเค็น OAuth 2.0 ไปพร้อมกับคำขอ แอปพลิเคชันจะส่งรหัสไคลเอ็นต์และรหัสลับไคลเอ็นต์ (หากมี) เพื่อรับโทเค็นก่อน คุณสามารถสร้างข้อมูลเข้าสู่ระบบ OAuth 2.0 สำหรับเว็บแอปพลิเคชัน บัญชีบริการ หรือแอปพลิเคชันที่ติดตั้ง

      หมายเหตุ: เนื่องจาก API นี้ไม่มีเมธอดใดที่ต้องใช้การให้สิทธิ์ OAuth 2.0 คุณจึงอาจต้องขอคีย์ API เท่านั้นตามที่อธิบายไว้ด้านล่าง อย่างไรก็ตาม หากแอปพลิเคชันเรียกใช้ API อื่นๆ ที่ต้องมีการให้สิทธิ์ของผู้ใช้ คุณยังคงต้องใช้ข้อมูลเข้าสู่ระบบ OAuth 2.0

      ดูข้อมูลเพิ่มเติมได้ที่เอกสารประกอบเกี่ยวกับ OAuth 2.0

    • คีย์ API: คําขอที่ไม่ได้ระบุโทเค็น OAuth 2.0 ต้องส่งคีย์ API คีย์จะระบุโปรเจ็กต์ของคุณ รวมถึงให้สิทธิ์เข้าถึง API, โควต้า และรายงาน

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

โปรดทําตามแนวทางปฏิบัติแนะนำสำหรับการใช้คีย์ API อย่างปลอดภัยเพื่อรักษาคีย์ API ให้ปลอดภัย

หลังจากมีคีย์ API แล้ว แอปพลิเคชันจะเพิ่มพารามิเตอร์การค้นหา key=yourAPIKey ต่อท้าย URL คำขอทั้งหมดได้

คีย์ API ปลอดภัยสำหรับการฝังใน URL โดยไม่จำเป็นต้องเข้ารหัส

ข้อจํากัดของคีย์ API

คีย์ API ของคุณจะไม่จำกัดโดยค่าเริ่มต้น ซึ่งไม่ปลอดภัยหากมีผู้อ่านคีย์นี้ (เช่น วางไว้ในเบราว์เซอร์) หรือเข้าถึงอุปกรณ์ที่มีคีย์อยู่ เราขอแนะนำให้คุณจำกัดคีย์ API นี้เพื่อยับยั้งการใช้งานที่ไม่ได้รับอนุญาต

หากต้องการเพิ่มข้อจำกัด ให้คลิกจํากัดคีย์ในกล่องโต้ตอบคีย์ API ที่สร้างขึ้น แผงการกําหนดค่าคีย์ API จะปรากฏขึ้น

ประเภทข้อจํากัดที่คุณเลือกจะขึ้นอยู่กับความต้องการในแอปพลิเคชัน ดังนี้

  • เว็บแอปพลิเคชันที่โต้ตอบกับ API โดยตรง (ไม่ใช่ผ่านแบ็กเอนด์หรือมิดเดิลแวร์) ควรเพิ่มข้อจํากัดผู้อ้างอิง HTTP อย่างไรก็ตาม โปรดทราบว่าแอปพลิเคชันดังกล่าวจะแสดงคีย์ API ของตนต่อสาธารณะ เราขอแนะนำให้ใช้รูปแบบการตรวจสอบสิทธิ์ของบัญชีบริการแทน
  • แอปพลิเคชันแบ็กเอนด์ที่ไม่สามารถรองรับบัญชีบริการได้ (เช่น อุปกรณ์แบบฝังที่ไม่มีภาษาที่รองรับในไลบรารีไคลเอ็นต์) ควรเพิ่มข้อจำกัดที่อยู่ IP เพื่อป้องกันการเข้าถึงจากไคลเอ็นต์ที่มีที่อยู่ IP ต่างกัน
  • แอปพลิเคชัน Android ควรเพิ่มข้อจำกัดแอป Android และเพิ่มชื่อแพ็กเกจและลายนิ้วมือใบรับรองที่ลงนาม SHA-1
  • แอปพลิเคชัน iOS ควรเพิ่มข้อจำกัดแอป iOS และเพิ่มตัวระบุชุด iOS เพื่อจำกัดการเรียก API ไปยังชุด iOS เหล่านี้

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

ตัวอย่าง electionQuery

ต่อไปนี้คือตัวอย่าง (โดยใช้ API เวอร์ชัน "v2") ของการเรียกใช้ electionQuery API เพื่อรับรายการรหัสการเลือกตั้งที่ถูกต้อง จากนั้นใช้ voterInfoQuery API กับที่อยู่จดทะเบียนของผู้เลือกเสียงเพื่อรับข้อมูลสำหรับการเลือกตั้งที่เลือก

ใช้ electionQuery เพื่อดูรายการรหัสการเลือกตั้งที่ถูกต้อง

  https://www.googleapis.com/civicinfo/v2/elections?key=<YOUR_API_KEY>
  

การตอบกลับ electionQuery:

{
 "kind": "civicinfo#electionsqueryresponse",
 "elections": [
  {
   "id": "2000",
   "name": "VIP Test Election",
   "electionDay": "2013-06-06"
  },
  {
   "id": "2124",
   "name": "Rhode Island 2012 Primary Election",
   "electionDay": "2012-09-11"
  },
  {
   "id": "2126",
   "name": "Delaware 2012 Primary Election",
   "electionDay": "2012-09-11"
  }
 ]
}

voterInfoQuery โดยใช้ curl

ใช้ curl เพื่อส่งคำขอ voterInfoQuery สำหรับรหัสการเลือกตั้งทดสอบ VIP 2000 และผู้เลือกตั้งที่ (ทดสอบ) ที่อยู่ 340 Main St, Venice, CA 90291 โปรดดูคำตอบ voterInfoQuery

curl "https://www.googleapis.com/civicinfo/v2/voterinfo?key=<YOUR_API_KEY>&address=340%20Main%20St.%20Venice%20CA&electionId=2000"

voterInfoQuery โดยใช้ไลบรารีของไคลเอ็นต์ Google API สําหรับ JavaScript

ตัวอย่างนี้ใช้ voterInfoQuery เดียวกันกับตัวอย่าง curl ก่อนหน้า แต่ใช้ไลบรารีไคลเอ็นต์ JavaScript คำตอบจาก voterInfoQuery เหมือนกับคำตอบตัวอย่างจาก curl

<!doctype html>
<html>
  <head>
    <script>
      /**
       * Build and execute request to look up voter info for provided address.
       * @param {string} address Address for which to fetch voter info.
       * @param {function(Object)} callback Function which takes the
       *     response object as a parameter.
       */
       function lookup(address, callback) {
       /**
         * Election ID for which to fetch voter info.
         * @type {number}
         */
        var electionId = 2000;
 
        /**
         * Request object for given parameters.
         * @type {gapi.client.HttpRequest}
         */
        var req = gapi.client.request({
            'path' : '/civicinfo/v2/voterinfo',
            'params' : {'electionId' : electionId, 'address' : address}
        });
       req.execute(callback);
      }

      /**
       * Render results in the DOM.
       * @param {Object} response Response object returned by the API.
       * @param {Object} rawResponse Raw response from the API.
       */
      function renderResults(response, rawResponse) {
        var el = document.getElementById('results');
        if (!response || response.error) {
          el.appendChild(document.createTextNode(
              'Error while trying to fetch polling place'));
          return;
        }
        var normalizedAddress = response.normalizedInput.line1 + ' ' +
            response.normalizedInput.city + ', ' +
            response.normalizedInput.state + ' ' +
            response.normalizedInput.zip;
        if (response.pollingLocations.length > 0) {
          var pollingLocation = response.pollingLocations[0].address;
          var pollingAddress = pollingLocation.locationName + ', ' +
              pollingLocation.line1 + ' ' +
              pollingLocation.city + ', ' +
              pollingLocation.state + ' ' +
              pollingLocation.zip;
          var normEl = document.createElement('strong');
          normEl.appendChild(document.createTextNode(
              'Polling place for ' + normalizedAddress + ': '));
          el.appendChild(normEl);
          el.appendChild(document.createTextNode(pollingAddress));
        } else {
          el.appendChild(document.createTextNode(
              'Could not find polling place for ' + normalizedAddress));
        }
      }

      /**
       * Initialize the API client and make a request.
       */
      function load() {
        gapi.client.setApiKey('YOUR API KEY GOES HERE');
        lookup('1263 Pacific Ave. Kansas City KS', renderResults);
      }
    </script>
    <script src="https://apis.google.com/js/client.js?onload=load"></script>
  </head>
  <body>
    <div id="results"></div>
  </body>
</html>

การตอบกลับ voterInfoQuery

{
  "kind": "civicinfo#voterinforesponse",
  "status": "success",
  "election": {
  "id": "2000",
  "name": "VIP Test Election",
  "electionDay": "2025-06-06",
  "ocdDivisionId": "ocd-division/country:us"
  },
  "normalizedInput": {
    "line1": "340 Main Street",
    "city": "Venice",
    "state": "CA",
    "zip": "90291"
  },
  "pollingLocations": [
    {
      "address": {
        "locationName": "WESTMINSTER AVENUE ELEMENTARY SCHOOL",
        "line1": "1010 ABBOT KINNEY BLVD",
        "city": "VENICE",
        "state": "CA",
        "zip": "90291"
      },
      "pollingHours": "",
      "latitude": 33.9919351,
      "longitude": -118.4722031,
      "startDate": "2024-03-05",
      "endDate": "2024-03-05",
      "sources": [
        {
          "name": "Voting Information Project",
          "official": true
        }
      ]
    },
    {
      "address": {
        "locationName": "POP UP VOTE CENTER 5",
        "line1": "12400 IMPERIAL HWY",
        "city": "NORWALK",
        "state": "CA",
        "zip": "90650"
      },
      "latitude": 33.915989,
      "longitude": -118.0677283,
      "sources": [
        {
          "name": "Voting Information Project",
          "official": true
        }
      ]
    }
  ],
  "dropOffLocations": [
    {
      "address": {
        "locationName": "FLEX VOTE CENTER 9",
        "line1": "12400 IMPERIAL HWY",
        "city": "NORWALK",
        "state": "CA",
        "zip": "90650"
      },
      "latitude": 33.915989,
      "longitude": -118.0677283,
      "sources": [
        {
          "name": "Voting Information Project",
          "official": true
        }
      ]
    },
  ],
  "contests": [
    {
      "type": "General",
      "ballotTitle": "UNITED STATES REPRESENTATIVE, 36th District",
      "district": {
        "name": "36TH US CONGRESSIONAL",
        "scope": "congressional"
      },
      "numberElected": "1",
      "numberVotingFor": "1",
      "ballotPlacement": "103",
      "sources": [
        {
          "name": "Voting Information Project",
          "official": true
        }
      ],
      "candidates": [
        {
          "name": "ARIANA HAKAMI",
          "party": "Party Preference: Republican"
        },
        {
          "name": "CLAIRE RAGGE ANDERSON",
          "party": "Party Preference: None"
        },
        {
          "name": "MELISSA TOOMIM",
          "party": "Party Preference: Republican"
        },
        {
          "name": "TED W. LIEU",
          "party": "Party Preference: Democratic"
        }
      ]
    },
    {
      "type": "ballot-measure",
      "ballotTitle": "LOS ANGELES CITY MUNICIPAL ELECTION - MEASURE HLA",
      "district": {
        "name": "CITY OF LOS ANGELES",
        "scope": "citywide"
      },
      "ballotPlacement": "116",
      "referendumTitle": "LOS ANGELES CITY MUNICIPAL ELECTION - MEASURE HLA",
      "referendumText": "CITY MOBILITY PLAN STREET IMPROVEMENT MEASURES. INITIATIVE ORDINANCE HLA. Shall an ordinance providing that when the City of Los Angeles makes a qualifying improvement to a City-owned street (e.g., a paving project), the City must also install certain street enhancements described in the City's Mobility Plan network of pedestrian, bicycle, transit, and vehicle routes; and requiring the City to provide publicly accessible information regarding street improvements; be adopted?",
      "referendumPassageThreshold": "MAJORITY OF VOTES CAST",
      "referendumBallotResponses": [
        "YES",
        "NO"
      ],
      "sources": [
        {
          "name": "Voting Information Project",
          "official": true
        }
      ]
    },
    {
      "type": "General",
      "ballotTitle": "DISTRICT ATTORNEY",
      "district": {
        "name": "LOS ANGELES COUNTY",
        "scope": "countywide"
      },
      "numberElected": "1",
      "numberVotingFor": "1",
      "ballotPlacement": "129",
      "sources": [
        {
          "name": "Voting Information Project",
          "official": true
        }
      ],
      "candidates": [
        {
          "name": "GEORGE GASCÓN"
        },
        {
          "name": "JONATHAN HATAMI"
        },
        {
          "name": "NATHAN HOCHMAN"
        },
        {
          "name": "DEBRA ARCHULETA"
        },
        {
          "name": "JEFF CHEMERINSKY"
        },
        {
          "name": "ERIC SIDDALL"
        },
        {
          "name": "MARIA RAMIREZ"
        },
        {
          "name": "DAN KAPELOVITZ"
        },
        {
          "name": "LLOYD \"BOBCAT\" MASSON"
        },
        {
          "name": "JOHN MCKINNEY"
        },
        {
          "name": "CRAIG J. MITCHELL"
        },
        {
          "name": "DAVID S. MILTON"
        }
      ]
    }
  ],
  "state": [
    {
      "name": "California",
      "electionAdministrationBody": {
        "name": "Secretary of State",
        "electionInfoUrl": "https://www.sos.ca.gov/elections/",
        "electionRegistrationUrl": "https://registertovote.ca.gov/?t=s",
        "electionRegistrationConfirmationUrl": "https://voterstatus.sos.ca.gov",
        "absenteeVotingInfoUrl": "https://elections.cdn.sos.ca.gov/vote-by-mail/replacement-application.pdf",
        "votingLocationFinderUrl": "https://voterstatus.sos.ca.gov",
        "ballotInfoUrl": "https://www.sos.ca.gov/elections/ballot-status/wheres-my-ballot/",
        "correspondenceAddress": {
          "line1": "1500 11th Street, 5th Floor",
          "city": "Sacramento",
          "state": "California",
          "zip": "95814"
        }
      },
      "local_jurisdiction": {
        "name": "Los Angeles",
        "electionAdministrationBody": {
          "name": "Registrar-Recorder/County Clerk",
          "electionInfoUrl": "http://www.lavote.gov/",
          "electionRegistrationUrl": "http://registertovote.ca.gov/",
          "electionRegistrationConfirmationUrl": "https://lavote.gov/vrstatus/",
          "absenteeVotingInfoUrl": "",
          "ballotInfoUrl": "http://www.lavote.gov/Locator",
          "physicalAddress": {
            "locationName": "Registrar-Recorder/County Clerk",
            "line1": "12400 Imperial Highway",
            "city": "Norwalk",
            "state": "CA",
            "zip": "90650"
          }
        },
        "sources": [
          {
            "name": "Voting Information Project",
            "official": true
          }
        ]
      }
    }
  ]
}