質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

Q&A

解決済

1回答

1541閲覧

[Gsuite] Groups Setting APIを実行すると結果がNULLで帰ってくる

Reiku

総合スコア40

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

API

APIはApplication Programming Interfaceの略です。APIはプログラムにリクエストされるサービスがどのように動作するかを、デベロッパーが定めたものです。

0グッド

0クリップ

投稿2020/05/26 02:41

前提・実現したいこと

GsuiteのGroups Setting APIを使い、Googleグループの設定情報を取得したい。

■ 前提
・PHPプログラムのバッチ処理として実装したい(not Webアプリケーション)
・PHP用のapi-clientを利用

bash

1# composer show -i 2firebase/php-jwt v5.2.0 A simple library to encode and decode JSON Web Tokens (JWT) in PHP. Should conform to the current spec. 3google/apiclient v2.4.1 Client library for Google APIs 4google/apiclient-services v0.137 Client library for Google APIs 5google/auth v1.9.0 Google Auth Library for PHP 6guzzlehttp/guzzle 5.3.4 Guzzle is a PHP HTTP client library and framework for building RESTful web service clients 7guzzlehttp/psr7 1.6.1 PSR-7 message implementation that also provides common utility methods 8guzzlehttp/ringphp 1.1.1 Provides a simple API and specification that abstracts away the details of HTTP into a single PHP function. 9guzzlehttp/streams 3.0.0 Provides a simple abstraction over streams of data 10monolog/monolog 1.25.4 Sends your logs to files, sockets, inboxes, databases and various web services 11phpseclib/phpseclib 2.0.27 PHP Secure Communications Library - Pure-PHP implementations of RSA, AES, SSH2, SFTP, X.509 etc. 12psr/cache 1.0.1 Common interface for caching libraries 13psr/http-message 1.0.1 Common interface for HTTP messages 14psr/log 1.1.3 Common interface for logging libraries 15ralouphie/getallheaders 2.0.5 A polyfill for getallheaders. 16react/promise v2.8.0 A lightweight implementation of CommonJS Promises/A for PHP

・Oauth認証はサービスアカウントを利用しています。

発生している問題・エラーメッセージ

認証エラー等はなく、APIから実行結果は帰ってくるのですが、
値がすべてNULLで帰ってきます。なぜでしょうか・・・

bash

1# php test.php 2object(Google_Service_Groupssettings_Groups)#72 (64) { 3 ["allowExternalMembers"]=> 4 NULL 5 ["allowGoogleCommunication"]=> 6 NULL 7 ["allowWebPosting"]=> 8 NULL 9 ["archiveOnly"]=> 10 NULL 11 ["customFooterText"]=> 12 NULL 13 ["customReplyTo"]=> 14 NULL 15 ["customRolesEnabledForSettingsToBeMerged"]=> 16 NULL 17 ["defaultMessageDenyNotificationText"]=> 18 NULL 19 ["description"]=> 20 NULL 21 ["email"]=> 22 NULL 23 ["enableCollaborativeInbox"]=> 24 NULL 25 ["favoriteRepliesOnTop"]=> 26 NULL 27 ["includeCustomFooter"]=> 28 NULL 29 ["includeInGlobalAddressList"]=> 30 NULL 31 ["isArchived"]=> 32 NULL 33 ["kind"]=> 34 NULL 35 ["maxMessageBytes"]=> 36 NULL 37 ["membersCanPostAsTheGroup"]=> 38 NULL 39 ["messageDisplayFont"]=> 40 NULL 41 ["messageModerationLevel"]=> 42 NULL 43 ["name"]=> 44 NULL 45 ["primaryLanguage"]=> 46 NULL 47 ["replyTo"]=> 48 NULL 49 ["sendMessageDenyNotification"]=> 50 NULL 51 ["showInGroupDirectory"]=> 52 NULL 53 ["spamModerationLevel"]=> 54 NULL 55 ["whoCanAdd"]=> 56 NULL 57 ["whoCanAddReferences"]=> 58 NULL 59 ["whoCanApproveMembers"]=> 60 NULL 61 ["whoCanApproveMessages"]=> 62 NULL 63 ["whoCanAssignTopics"]=> 64 NULL 65 ["whoCanAssistContent"]=> 66 NULL 67 ["whoCanBanUsers"]=> 68 NULL 69 ["whoCanContactOwner"]=> 70 NULL 71 ["whoCanDeleteAnyPost"]=> 72 NULL 73 ["whoCanDeleteTopics"]=> 74 NULL 75 ["whoCanDiscoverGroup"]=> 76 NULL 77 ["whoCanEnterFreeFormTags"]=> 78 NULL 79 ["whoCanHideAbuse"]=> 80 NULL 81 ["whoCanInvite"]=> 82 NULL 83 ["whoCanJoin"]=> 84 NULL 85 ["whoCanLeaveGroup"]=> 86 NULL 87 ["whoCanLockTopics"]=> 88 NULL 89 ["whoCanMakeTopicsSticky"]=> 90 NULL 91 ["whoCanMarkDuplicate"]=> 92 NULL 93 ["whoCanMarkFavoriteReplyOnAnyTopic"]=> 94 NULL 95 ["whoCanMarkFavoriteReplyOnOwnTopic"]=> 96 NULL 97 ["whoCanMarkNoResponseNeeded"]=> 98 NULL 99 ["whoCanModerateContent"]=> 100 NULL 101 ["whoCanModerateMembers"]=> 102 NULL 103 ["whoCanModifyMembers"]=> 104 NULL 105 ["whoCanModifyTagsAndCategories"]=> 106 NULL 107 ["whoCanMoveTopicsIn"]=> 108 NULL 109 ["whoCanMoveTopicsOut"]=> 110 NULL 111 ["whoCanPostAnnouncements"]=> 112 NULL 113 ["whoCanPostMessage"]=> 114 NULL 115 ["whoCanTakeTopics"]=> 116 NULL 117 ["whoCanUnassignTopic"]=> 118 NULL 119 ["whoCanUnmarkFavoriteReplyOnAnyTopic"]=> 120 NULL 121 ["whoCanViewGroup"]=> 122 NULL 123 ["whoCanViewMembership"]=> 124 NULL 125 ["internal_gapi_mappings":protected]=> 126 array(0) { 127 } 128 ["modelData":protected]=> 129 array(0) { 130 } 131 ["processed":protected]=> 132 array(0) { 133 } 134}

該当のソースコード

PHP

1<?php 2// read api-client 3require_once '[PATH_TO_LIB]/vendor/autoload.php'; 4// Service Account Credential 5putenv('GOOGLE_APPLICATION_CREDENTIALS=[PATH_TO_CREDENTIAL_DIR]/service-account.json'); 6 7# -------------------------------------- 8# Set API client. 9# -------------------------------------- 10 11$client = new Google_Client(); 12 13// set Application Name 14$client->setApplicationName("google_group_setting_api"); 15 16// set Credential 17$client->useApplicationDefaultCredentials(); 18 19// set Scope 20$client->setScopes(array( 21 'https://www.googleapis.com/auth/apps.groups.settings' 22)); 23// set User (AdminUser) 24$client->setSubject('[Auth UserAccount]'); 25 26# --------------------------------------- 27# construct the service object. 28# -------------------------------------- 29 30$service = new Google_Service_Groupssettings($client); 31 32# ------------------------------------- 33# Get Group Setting 34# ------------------------------------- 35 36$groupKey = "[Group MailAddress]"; 37 38$settings = $service->groups->get($groupKey); 39 40var_dump($settings); 41 42?>

試したこと

公式リファレンスのAPI Explorerから同様のAPIをOAuthで認証し、実行すると、
正しく結果を取得することができました。
現在、サービスアカウントを利用していることが原因の線も調査しています。

補足情報(FW/ツールのバージョンなど)

・OS / PHPバージョン

bash

1# cat /etc/redhat-release 2CentOS Linux release 7.5.1804 (Core) 3# php -v 4PHP 5.4.16 (cli) (built: Nov 1 2019 16:04:20)

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

自己解決しました。
どうやら、実行結果をjsonで帰ってくるように指定しないと、正しく結果が取得できないようです。

php

1// [重要] jsonで帰ってくるように指定 2$optParams = ["alt" => "json"]; 3 4$groupKey = "[Group MailAddress]"; 5 6$settings = $service->groups->get($groupKey$optParams); 7 8var_dump($settings); 9

結局GoogleのPHPライブラリを解読する形になりましたが、
decodeHttpResponce関数内で、
戻り値の型を指定された場合にjson_decodeを行っています。

php

1// vendor/google/apiclient/src/Google/Http/REST.php 2// line103~ 3 public static function decodeHttpResponse( 4 ResponseInterface $response, 5 RequestInterface $request = null, 6 $expectedClass = null 7 ) { 8 $code = $response->getStatusCode(); 9 // retry strategy 10 if (intVal($code) >= 400) { 11 // if we errored out, it should be safe to grab the response body 12 $body = (string) $response->getBody(); 13 14 // Check if we received errors, and add those to the Exception for convenience 15 throw new Google_Service_Exception($body, $code, null, self::getResponseErrors($body)); 16 } 17 18 // Ensure we only pull the entire body into memory if the request is not 19 // of media type 20 $body = self::decodeBody($response, $request); 21 if ($expectedClass = self::determineExpectedClass($expectedClass, $request)) { 22 $json = json_decode($body, true); 23 return new $expectedClass($json); 24 } 25 return $response; 26 }

何も指定していない場合、xml形式でレスポンスが返ってきて、
それをjson_decodeに突っ込むことで処理ができずにNULLになっていたようです。

投稿2020/05/28 08:04

編集2020/05/28 08:08
Reiku

総合スコア40

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問