推送注册.create不适用于通过域范围内的委托服务帐户生成的令牌

时间:2018-11-07 18:48:00

标签: google-cloud-pubsub google-classroom

Push Registration.create不适用于通过域范围内的委托服务帐户生成的令牌。 registration.create返回成功的响应,但是没有通知发送到pubsub主题。

Registration.create的响应:

{
    "registrationId": "-4693431763215581184",
    "feed": {
        "feedType": "COURSE_WORK_CHANGES",
        "courseWorkChangesInfo": {
            "courseId": "24492527610"
        }
    },
    "cloudPubsubTopic": {
        "topicName": "projects/littlesis-cloud/topics/watch-classroom"
    },
    "expiryTime": "2018-11-14T16:48:50.340Z"
}

如果我在Apps脚本中进行相同的Registration.create,则会得到相同的响应:

{
feed:
   {
  feedType:COURSE_WORK_CHANGES, 
  courseWorkChangesInfo:
    {courseId:24492527610}
   }, 
 cloudPubsubTopic:{topicName:projects/littlesis-cloud/topics/watch-classroom},
 registrationId:-4693431763215581184,
 expiryTime:2018-11-14T16:50:56.819Z
}

但是现在在Apps脚本中运行Registrations.create之后,我得到了推送通知。注意其相同的registrationId。

我两次都使用相同的超级管理员帐户。服务帐户,应用程序脚本项目和pubsub主题都在同一个Console项目中。服务帐户生成的应用程序脚本和令牌都具有相同的作用域。服务帐户在管理控制台中具有相同的授予范围。

在NodeJS中生成令牌方法:

function setUser(primaryEmail){
return new Promise((resolve, reject) => {
      let jwtClient = {};
      try {
        jwtClient = new google.auth.JWT({
            key:     this.serviceAccount.private_key,
            email:   this.serviceAccount.client_email,
            scopes:  [
              "https://www.googleapis.com/auth/classroom.courses",
              "https://www.googleapis.com/auth/classroom.profile.emails",
              "https://www.googleapis.com/auth/classroom.profile.photos",
              "https://www.googleapis.com/auth/classroom.push-notifications",
              "https://www.googleapis.com/auth/classroom.rosters",
              "https://www.googleapis.com/auth/classroom.coursework.students"
            ],
            subject: primaryEmail || this.serviceAccount.client_email,
          },
        );
      }
      catch (error) {
        console.error(error);
        throw new Error(error);
      }

      jwtClient.authorize((err) => {
        if (err) {
          console.log(err);
          reject(err);
        } else {
          this.client = jwtClient;
          this.classroom = google.classroom({version: 'v1', auth: jwtClient});
          resolve(jwtClient);
        }
      });
    });
}

createCourseWorkRegistration(pubSubTopic, courseId){
    let postBody = {
      feed:{
        feedType: 'COURSE_WORK_CHANGES',
        courseWorkChangesInfo:{
          courseId: courseId
        }
      },
      cloudPubsubTopic: {
        topicName: pubSubTopic
      }
    };

    let options = {
      resource: postBody
    };
    return this.classroom.registrations.create(options)
  }

应用脚本方法

function myFunction() {
  var resp = Classroom.Registrations.create(
   {
       feed:{
        feedType: 'COURSE_WORK_CHANGES',
        courseWorkChangesInfo:{
          courseId: '24492527610'
        }
       },
      cloudPubsubTopic: {
        topicName: 'projects/littlesis-cloud/topics/watch-classroom'
      }   
   }
  );
  console.log(resp)
  Logger.log(resp)
}

预期输出是多少?你看到了什么呢?如果您看到错误消息,请提供它们。

我希望收到推送通知。相反,我什么也没得到。没有错误消息。我想念什么?

0 个答案:

没有答案