"구글 API 튜토리얼"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-<source +<syntaxhighlight , -</source> +</syntaxhighlight>))
 
(사용자 2명의 중간 판 33개는 보이지 않습니다)
1번째 줄: 1번째 줄:
==개요==
;Google API tutorial
;Google API tutorial
;구글 API 시작하기, 구글 API 튜토리얼
;구글 API 시작하기, 구글 API 튜토리얼
;구글API PHP 클라이언트 시작하기
;google-api-php-client 사용해보기
* google-api-php-client를 설치하고, 포함된 예제를 하나 실행해보자.


==API 키 발급==
==사전작업==
*https://console.developers.google.com/project 접속
*[[구글 API 키 발급]]
*API Project 클릭
*왼쪽 메뉴(Sidebar)에서 Credentials 클릭
*OAuth 아래의 [Create new Client ID] 버튼 클릭
*Application type 선택: (O) Service account<ref>Web application, Service account, Installed application 중 선택. 여기서는 필자의 목적에 맞게 Service account를 선택함</ref> --- [Create Client ID] 버튼 클릭
*"New Public/Private key pair generated" 창과 함께 json 파일이 다운로드됨 --- [Okay, got it]
:화면에 표시되는 내용은...
{| class='wikitable'
|+Service Account
|-
! Client ID
| 871045965516-m6pxhmgmk0f6l6cdeq5alsbwh9own7ma.apps.googleusercontent.com
|-
! Email address
| 871045965516-m6pxhmgmk0f6l6cdeq5alsbwh9own7ma@developer.gserviceaccount.com
|-
! Certificate fingerprints
| tl28322d248g14q2kxt37d3253981ap8l878dyy3
|}


{| class='wikitable'
==설치요건 확인==
|+Key for server applications
|-
! API key
| AIzaSyBDEuxz8GIb0K2df4_hjlMOQSU68npWYrA
|-
! IPs
| 135.79.246.80/32
|-
! Activation date
| Jan 22, 2015, 8:55:00 PM
|-
! Activated by
| example@gmail.com (you)
|}
 
:"API Project-6bd80f24cead.json" 파일의 내용은... (본 실습에서는 이 파일이 필요없지만 살펴보고 가자.)
<source lang='javascript'>
{
  "private_key_id": "tl28322d248g14q2kxt37d3253981ap8l878dyy3",
  "private_key": "-----BEGIN PRIVATE KEY-----\nMIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAM86vrJUfFHuzcqW\nByp+w+nWAI6iFzpaYiLTaXp1pjrRLmwAWfO094y1qmaO7vvCEFt06yGb1v5VkNg7\n2mVdTzpdCdYnpv2q1wUQ6x2O2p4jISgrocvigQXvGdsDA1HReNXRyV1kxZJbwA81\nuxH9edFCbCZW9/HMaEN0IhGm6iebAgMBAAECgYAVOTb+nJh+66436VWijIkUEc7M\nzN0EJO2YE37iA9qae4H9nauYwhf4WUKybkEbl6yFMVxDfOpv8eN7weWELCJI8LxR\nqTiw/f+BQnHyqSrvaY8x8siD7Z+1ut6ZCmIMwbRAx/OqVe0R23pfpuSblvgHYuW/\nI5TNxKytQo9HsbsmQQJBAOyNbA3GMj4QLwdQw/1EwnLLY/255Zrplgtv19PKrZ+N\nPvOb7v5dLzDaX0LaMnxqFK77WSf0GwO8ZWgfnkHZ3EUCQQDgRC4V0l6Lk0xaCJxF\ns27kkIWNnwYVaYpWOeHjbq5tE3dHlMgLzurtZ9s6a5w9faT4PcSW6Yv4ybIjVL+j\nM2JfAkBR7WEhHCXGjQBcvkJC84+NBphpBftZvdDzwRa1CZs8iUTmRnltq4RunRMl\nKkEWCf+D4xWr2sWIc7WDbH/5OqUpAkEAqa+MuyAgPzVlWN4Y97gLWM9s7bn8DWl8\nrT9AhifxvEA1SSjur9iQ8q5ZNvvDDNerYdTCkXkGWmip64etNFuluwJANo+7AVcO\nntq82SIVLV/MrTO1ZpIlIM3dJSPmFCBGtYWckr7uwi5QZp7VjZFdFcsh84LqVQdC\n3tDhhDau9E9+9w\u003d\u003d\n-----END PRIVATE KEY-----\n",
  "client_email": "871045965516-m6pxhmgmk0f6l6cdeq5alsbwh9own7ma@developer.gserviceaccount.com",
  "client_id": "871045965516-m6pxhmgmk0f6l6cdeq5alsbwh9own7ma.apps.googleusercontent.com",
  "type": "service_account"
}
</source>
 
==구글API PHP 클라이언트 설치 요건 확인==
{{참고|PHP 버전 확인}}
{{참고|PHP 버전 확인}}
{{참고|PHP 모듈 설치 확인}}
{{참고|PHP 모듈 설치 확인}}
*PHP 버전 5.2.1 이상, JSON 확장기능<ref>https://github.com/google/google-api-php-client</ref>
*PHP 버전 5.2.1 이상, JSON 확장기능<ref>https://github.com/google/google-api-php-client</ref>
<source lang='cli'>
<syntaxhighlight lang='console'>
[root@zetawiki ~]# php -v
root@zetawiki:~# php -v
PHP 5.3.3 (cli) (built: Oct 30 2014 20:12:53)  
PHP 5.5.9-1ubuntu4.16 (cli) (built: Apr 20 2016 14:31:27)  
Copyright (c) 1997-2010 The PHP Group
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
</source>
    with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies
<source lang='cli'>
</syntaxhighlight>
[root@zetawiki ~]# php -m | grep json
<syntaxhighlight lang='console'>
root@zetawiki:~# php -m | grep json
json
json
</source>
</syntaxhighlight>
 
==composer 설치==
{{참고|composer 설치}}


==git으로 구글API 클라이언트 설치==
==composer로 설치==
<source lang='cli'>
* [[/usr/share/php/]]로 이동하여 composer로 구글 API 클라이언트 설치
[root@zetawiki ~]# git clone https://github.com/google/google-api-php-client.git
<syntaxhighlight lang='console'>
Initialized empty Git repository in /root/google-api-php-client/.git/
root@zetawiki:~# cd /usr/share/php/
remote: Counting objects: 7688, done.
root@zetawiki:/usr/share/php# rm -f composer.*
remote: Compressing objects: 100% (35/35), done.
root@zetawiki:/usr/share/php# composer require 'google/apiclient'
remote: Total 7688 (delta 28), reused 0 (delta 0), pack-reused 7646
Using version ^1.1 for google/apiclient
Receiving objects: 100% (7688/7688), 5.38 MiB | 972 KiB/s, done.
./composer.json has been created
Resolving deltas: 100% (5342/5342), done.
Loading composer repositories with package information
</source>
Updating dependencies (including require-dev)
  - Installing google/apiclient (1.1.7)
    Downloading: 100%        


==PHP 라이브러리 폴더로 이동==
Writing lock file
{{참고|PHP 라이브러리 폴더}}
Generating autoload files
<source lang='cli'>
</syntaxhighlight>
[root@zetawiki ~]# mv ./google-api-php-client/src/Google/ /usr/share/php/vendor/
* 폴더 이동
[root@zetawiki ~]# ll /usr/share/php/vendor/ | grep Google
<syntaxhighlight lang='console'>
drwxr-xr-x 12 root root 4096 Jun 22 22:11 Google
root@zetawiki:/usr/share/php# mv vendor/ google-api
</source>
root@zetawiki:/usr/share/php#
</syntaxhighlight>


==예제 실행==
==인클루드 테스트==
*예제 batch.php를  my_book_client.php로 복제하여 테스트
* 홈폴더로 이동하여 테스트 파일 작성
*라이브러리 위치를 맞춰줌
<syntaxhighlight lang='console'>
<source lang='cli'>
root@zetawiki:/usr/share/php# cd
[root@zetawiki ~]# cd google-api-php-client/examples/
root@zetawiki:~# vi google-api_test.php
[root@zetawiki examples]# cp batch.php my_book_client.php
</syntaxhighlight>
[root@zetawiki examples]# sed -i 's/<YOUR_API_KEY>/AIzaSyBDEuxz8GIb0K2df4_hjlMOQSU68npWYrA/g' my_book_client.php
<syntaxhighlight lang='php'>
[root@zetawiki examples]# sed -i "s/require_once realpath(dirname(__FILE__) . '\/..\/src\/Google\/autoload.php');/include '\/usr\/share\/php\/vendor\/Google\/autoload.php';/g" my_book_client.php
<?php
[root@zetawiki examples]# diff batch.php my_book_client.php
include 'google-api/autoload.php';
25c25
$client = new Google_Client();
< require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php');
var_dump( $client );
---
</syntaxhighlight>
> include '/usr/share/php/vendor/Google/autoload.php';
<syntaxhighlight lang='console'>
35c35
root@zetawiki:~# php google-api_test.php | head
< $apiKey = "<YOUR_API_KEY>"; // Change to your API key.
Google_Client Object
---
(
> $apiKey = "AIzaSyBDEuxz8GIb0K2df4_hjlMOQSU68npWYrA"; // Change to your API key.
    [auth:Google_Client:private] =>
</source>
    [io:Google_Client:private] =>
<source lang='cli'>
    [cache:Google_Client:private] =>
[root@zetawiki examples]# php my_book_client.php  
    [config:Google_Client:private] => Google_Config Object
<h3>Results Of Call 1:</h3>The Writings of Henry David Thoreau<br />  
        (
Walden<br />  
            [configuration:protected] => Array
                (
                    [application_name] =>
</syntaxhighlight>
:→ 이상없음
 
==batch.php 테스트==
* 예제 batch.php를 홈폴더로 복제·수정하여 테스트
* batch.php를 편집하여 라이브러리 위치를 맞춰주고, apiKey 입력
<syntaxhighlight lang='console'>
root@zetawiki:~# cp /usr/share/php/google-api/google/apiclient/examples/batch.php .
root@zetawiki:~# vi batch.php
</syntaxhighlight>
<syntaxhighlight lang='php' highlight='2,3,11,12,22,23'>
... (생략)
#### include_once "templates/base.php";
include_once "google-api/google/apiclient/examples/templates/base.php";
echo pageHeader("Batching Queries");
 
/************************************************
  We're going to use the simple access to the
  books API again as an example, but this time we
  will batch up two queries into a single call.
************************************************/
#### require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php');
require_once "google-api/autoload.php";
 
/************************************************
  We create the client and set the simple API
  access key. If you comment out the call to
  setDeveloperKey, the request may still succeed
  using the anonymous quota.
************************************************/
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
#### $apiKey = "<YOUR_API_KEY>"; // Change to your API key.
$apiKey = "AIzaSyBDEuxz8GIb0K2df4_hjlMOQSU68npWYrA";
... (생략)
</syntaxhighlight>
 
==테스트==
<syntaxhighlight lang='console'>
root@zetawiki:~# php batch.php  
<h3>Results Of Call 1:</h3>Walden<br />
The Writings of Henry David Thoreau<br />  
The Maine Woods<br />  
A Week on the Concord and Merrimack Rivers<br />  
A Week on the Concord and Merrimack Rivers<br />  
... (생략)
Excursions<br />
Letters to Various Persons<br />
A Yankee in Canada<br />
A Hand-book of English Literature<br />
Neota Wilderness<br />
First Editions of American Authors<br />
<h3>Results Of Call 2:</h3>Catalog of Copyright Entries. Third Series<br />
English Literature of the 19th & 20th Centuries...<br />
Catalog of Copyright Entries. Part 1. [C] Group 3. Dramatic Composition and Motion Pictures. New Series<br />
Catalog of Copyright Entries. Part 1. [A] Group 1. Books. New Series<br />
Catalog of Copyright Entries. New Series<br />
Catalog of copyright entries<br />
Catalogue of English literature, comprising early plays, balads, poetry from Chaucer to Swinburne, books with colored plates, first editions, association books, authors' manuscripts, autograph letters...<br />
Catalog of Copyright Entries. Third Series<br />
Catalog of Copyright Entries<br />  
Catalog of Copyright Entries<br />  
Catalog of Copyright Entries. Part 1. [A] Group 1. Books. New Series<br />
Catalogue of Copyright Entries<br />
小泉信三文庫目録<br />  
小泉信三文庫目録<br />  
</source>
</syntaxhighlight>
:→ Book API가 잘 수행되었다. 다른 API도 사용해보자.<ref>사용전에 Enable 절차가 필요할 수 있다.</ref>
:→ Book API가 잘 수행되었다. 다른 API도 사용해보자.<ref>단, API 종류에 따라 사용전 Enable 절차가 필요할 수 있다.</ref>


==같이 보기==
==같이 보기==
*[[구글 API 튜토리얼 2 - ISBN으로 책정보 조회]]
*[[구글 API 튜토리얼 2 - ISBN으로 책정보 조회]]
*[[구글 애널리틱스 API 시작하기]]
*[[구글 애널리틱스 API 시작하기]]
 
*[[The calling IP address 135.79.246.80 does not match the IP restrictions configured on the API key. Please use the API Console to update your key restrictions.]]
==주석==
<references/>


==주석==
==주석==

2021년 9월 24일 (금) 23:27 기준 최신판

1 개요[ | ]

Google API tutorial
구글 API 시작하기, 구글 API 튜토리얼
구글API PHP 클라이언트 시작하기
google-api-php-client 사용해보기
  • google-api-php-client를 설치하고, 포함된 예제를 하나 실행해보자.

2 사전작업[ | ]

3 설치요건 확인[ | ]

  • PHP 버전 5.2.1 이상, JSON 확장기능[1]
root@zetawiki:~# php -v
PHP 5.5.9-1ubuntu4.16 (cli) (built: Apr 20 2016 14:31:27) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies
root@zetawiki:~# php -m | grep json
json

4 composer 설치[ | ]

5 composer로 설치[ | ]

  • /usr/share/php/로 이동하여 composer로 구글 API 클라이언트 설치
root@zetawiki:~# cd /usr/share/php/
root@zetawiki:/usr/share/php# rm -f composer.*
root@zetawiki:/usr/share/php# composer require 'google/apiclient'
Using version ^1.1 for google/apiclient
./composer.json has been created
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing google/apiclient (1.1.7)
    Downloading: 100%         

Writing lock file
Generating autoload files
  • 폴더 이동
root@zetawiki:/usr/share/php# mv vendor/ google-api
root@zetawiki:/usr/share/php#

6 인클루드 테스트[ | ]

  • 홈폴더로 이동하여 테스트 파일 작성
root@zetawiki:/usr/share/php# cd
root@zetawiki:~# vi google-api_test.php
<?php
include 'google-api/autoload.php';
$client = new Google_Client();
var_dump( $client );
root@zetawiki:~# php google-api_test.php | head 
Google_Client Object
(
    [auth:Google_Client:private] => 
    [io:Google_Client:private] => 
    [cache:Google_Client:private] => 
    [config:Google_Client:private] => Google_Config Object
        (
            [configuration:protected] => Array
                (
                    [application_name] =>
→ 이상없음

7 batch.php 테스트[ | ]

  • 예제 batch.php를 홈폴더로 복제·수정하여 테스트
  • batch.php를 편집하여 라이브러리 위치를 맞춰주고, apiKey 입력
root@zetawiki:~# cp /usr/share/php/google-api/google/apiclient/examples/batch.php .
root@zetawiki:~# vi batch.php
... (생략)
#### include_once "templates/base.php";
include_once "google-api/google/apiclient/examples/templates/base.php";
echo pageHeader("Batching Queries");

/************************************************
  We're going to use the simple access to the
  books API again as an example, but this time we
  will batch up two queries into a single call.
 ************************************************/
#### require_once realpath(dirname(__FILE__) . '/../src/Google/autoload.php');
require_once "google-api/autoload.php";

/************************************************
  We create the client and set the simple API
  access key. If you comment out the call to
  setDeveloperKey, the request may still succeed
  using the anonymous quota.
 ************************************************/
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
#### $apiKey = "<YOUR_API_KEY>"; // Change to your API key.
$apiKey = "AIzaSyBDEuxz8GIb0K2df4_hjlMOQSU68npWYrA";
... (생략)

8 테스트[ | ]

root@zetawiki:~# php batch.php 
<h3>Results Of Call 1:</h3>Walden<br /> 
The Writings of Henry David Thoreau<br /> 
The Maine Woods<br /> 
A Week on the Concord and Merrimack Rivers<br /> 
Excursions<br /> 
Letters to Various Persons<br /> 
A Yankee in Canada<br /> 
A Hand-book of English Literature<br /> 
Neota Wilderness<br /> 
First Editions of American Authors<br /> 
<h3>Results Of Call 2:</h3>Catalog of Copyright Entries. Third Series<br /> 
English Literature of the 19th & 20th Centuries...<br /> 
Catalog of Copyright Entries. Part 1. [C] Group 3. Dramatic Composition and Motion Pictures. New Series<br /> 
Catalog of Copyright Entries. Part 1. [A] Group 1. Books. New Series<br /> 
Catalog of Copyright Entries. New Series<br /> 
Catalog of copyright entries<br /> 
Catalogue of English literature, comprising early plays, balads, poetry from Chaucer to Swinburne, books with colored plates, first editions, association books, authors' manuscripts, autograph letters...<br /> 
Catalog of Copyright Entries. Third Series<br /> 
Catalog of Copyright Entries<br /> 
小泉信三文庫目録<br />
→ Book API가 잘 수행되었다. 다른 API도 사용해보자.[2]

9 같이 보기[ | ]

10 주석[ | ]

  1. https://github.com/google/google-api-php-client
  2. 단, API 종류에 따라 사용전 Enable 절차가 필요할 수 있다.
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}