Mendapatkan Token Akses

Untuk memanggil Indexing API, panggilan harus diautentikasi dengan token OAuth yang Anda dapatkan sebagai penukar kunci pribadi Anda. Google menyediakan library klien API untuk mendapatkan token OAuth untuk sejumlah bahasa.

Persyaratan

Saat mengirim permintaan ke Indexing API, permintaan Anda harus:

  1. Menggunakan https://www.googleapis.com/auth/indexing sebagai cakupannya.
  2. Menggunakan salah satu endpoint yang dijelaskan dalam Menggunakan API.
  3. Menyertakan token akses akun layanan.
  4. Menentukan isi permintaan seperti yang dijelaskan dalam Menggunakan API.

Contoh

Contoh berikut menunjukkan cara mendapatkan token akses OAuth:

Python

Mendapatkan token OAuth menggunakan Library Klien Google API untuk Python:

from oauth2client.service_account import ServiceAccountCredentials
import httplib2

SCOPES = [ "https://www.googleapis.com/auth/indexing" ]
ENDPOINT = "https://indexing.googleapis.com/v3/urlNotifications:publish"

# service_account_file.json is the private key that you created for your service account.
JSON_KEY_FILE = "service_account_file.json"

credentials = ServiceAccountCredentials.from_json_keyfile_name(JSON_KEY_FILE, scopes=SCOPES)

http = credentials.authorize(httplib2.Http())

// Define contents here. This example shows a simple update request. Other types
// of requests are described in the next step.
content = "{
  \"url\": \"http://example.com/jobs/42\",
  \"type\": \"URL_UPDATED"
}"

response, content = http.request(ENDPOINT, method="POST", body=content)

Java

Mendapatkan token OAuth menggunakan Library Klien API untuk Java:

String scopes = "https://www.googleapis.com/auth/indexing";
String endPoint = "https://indexing.googleapis.com/v3/urlNotifications:publish";

JsonFactory jsonFactory = new JacksonFactory();

// service_account_file.json is the private key that you created for your service account.
InputStream in = IOUtils.toInputStream("service_account_file.json");

GoogleCredential credentials =
  GoogleCredential.fromStream(in, this.httpTransport, jsonFactory).createScoped(Collections.singleton(scopes));

GenericUrl genericUrl = new GenericUrl(endPoint);
HttpRequestFactory requestFactory = this.httpTransport.createRequestFactory();

// Define content here. The structure of the content is described in the next step.
String content = "{"
  + "\"url\": \"http://example.com/jobs/42\","
  + "\"type\": \"URL_UPDATED\","
  + "}";

HttpRequest request =
  requestFactory.buildPostRequest(genericUrl, ByteArrayContent.fromString("application/json", content));

credentials.initialize(request);
HttpResponse response = request.execute();
int statusCode = response.getStatusCode();

PHP

Mendapatkan token OAuth menggunakan Library Klien API untuk PHP:

require_once 'google-api-php-client/vendor/autoload.php';

$client = new Google_Client();

// service_account_file.json is the private key that you created for your service account.
$client->setAuthConfig('service_account_file.json');
$client->addScope('https://www.googleapis.com/auth/indexing');

// Get a Guzzle HTTP Client
$httpClient = $client->authorize();
$endpoint = 'https://indexing.googleapis.com/v3/urlNotifications:publish';

// Define contents here. The structure of the content is described in the next step.
$content = "{
  \"url\": \"http://example.com/jobs/42\",
  \"type\": \"URL_UPDATED"
}";

$response = $httpClient->post($endpoint, [ 'body' => $content ]);
$status_code = $response->getStatusCode();

Node.js

Mendapatkan token OAuth menggunakan Library Klien Node.js:

var request = require("request");
var google = require("googleapis");
var key = require("./service_account.json");

const jwtClient = new google.auth.JWT(
  key.client_email,
  null,
  key.private_key,
  ["https://www.googleapis.com/auth/indexing"],
  null
);

jwtClient.authorize(function(err, tokens) {
  if (err) {
    console.log(err);
    return;
  }
  let options = {
    url: "https://indexing.googleapis.com/v3/urlNotifications:publish",
    method: "POST",
    // Your options, which must include the Content-Type and auth headers
    headers: {
      "Content-Type": "application/json"
    },
    auth: { "bearer": tokens.access_token },
    // Define contents here. The structure of the content is described in the next step.
    json: {
      "url": "http://example.com/jobs/42",
      "type": "URL_UPDATED"
    };
  request(options, function (error, response, body) {
    // Handle the response
    console.log(body);
  });
});

Selain menunjukkan cara mendapatkan token, contoh tersebut menunjukkan tempat Anda dapat menambahkan bagian isi pesan permintaan. Untuk informasi tentang jenis panggilan yang dapat Anda buat, dan struktur bagian isi pesan untuk panggilan tersebut, lihat Menggunakan API.