【问题标题】:How does the Google Service Client (PHP) method documentation work?Google 服务客户端 (PHP) 方法文档如何工作?
【发布时间】:2020-05-30 16:21:36
【问题描述】:

我在使用任何未在某处明确举例说明的客户端服务方法时都遇到困难。尽管遵循了文档,甚至阅读了源代码,但我提出的遵循方案的类或方法名永远不会正确。

Packagist 的文档(请参阅“发出请求”)说客户端库类是从 Google 端点自动生成的,这与 library's docs on Github 中的描述一致,即访问方法的模式应该是“@987654324 @"。

那么为什么会出现以下情况?

// works:
// I get a countable object of active classrooms owner by the specified id
$response = $this->ClassroomService->courses->listCourses([
            'courseStates' =>   'ACTIVE',
            'teacherId'     => 'me']);

// works:
// I get an instance of the single classroom's object containing lots of meta data
$response = $this->ClassroomService->courses->get( $id );

// does not work:
// 500 error, obj has no such method
$response = $this->ClassroomService->topics->listCoursesTopics( $id  );

根据API Explorer,这三个都应该没问题。

我在使用客户端服务对象时缺少什么?

编辑 最终,我将示例中的资源确定为“courses_topics”;根据文档,该方法是正确的。感谢@ebram 的想法。 问题仍然是这些方法是如何命名的。 courseWork 是我的下一个挑战,它不符合 topics 的命名模式。

【问题讨论】:

  • 也许尝试 var_dump 主题? :P 这就是我真正陷入困境时所做的事情

标签: php google-api google-api-php-client google-classroom


【解决方案1】:
  • topics 没有ClassroomService 的成员。
  • 成员名为courses_topics

您的代码应如下所示:

$response = $this->ClassroomService->courses_topics->listCoursesTopics( $id  );
  • Google_Service_Classroom_CoursesTopics_Resource 的文档确实错误地提供了成员名为 ->topics 的示例代码。
  • 鉴于 API 文档是从源代码生成的,但示例代码(通常)是手工编写的,我会假设 API 文档是正确的,而示例代码是不正确的。我会向 Google 提交文档错误。

更新:

我查看了the PHP source code for Google_Service_Classroom in GitHub 并验证该属性名为courses_topics 而不是topics,因此结论是:示例代码错误

有趣的是实际源代码中的资源类型是Google_Service_Classroom_Resource_CoursesTopics,但文档将其称为Google_Service_Classroom_CoursesTopics_Resource - 所以文档肯定是错误的。

【讨论】:

  • 感谢您的帮助,尽管我的问题编辑表明我也发现了这一点。问题实际上是针对记录方法的位置。我现在尝试运行 listCourseWork() 时遇到了完全相同的困难。文档使它看起来像service->courseWork->listCoursesCourseWork() 是一件事,同时主题示例让我尝试service->course_work->listCoursesCourseWork() ...两者都会产生未定义的属性错误。这些是不是没有规范的命名!?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-03
  • 1970-01-01
  • 1970-01-01
  • 2017-04-02
  • 2011-09-09
  • 2016-08-06
相关资源
最近更新 更多