为我的Google文档插件

时间:2016-03-09 15:25:40

标签: google-apps-script google-apps google-apps-marketplace

我们为Google Apps Marketplace SDK启用了Google-docs插件(基于Google Apps脚本),以便Google Apps管理员可以在域级别安装我们的插件。

我们已经注意到一些域已经安装了我们的附加组件 - 但我似乎无法找到一种方法来获取有关哪些域安装了我们的信息。它甚至可能吗?

我已尝试过Marketplace许可API https://developers.google.com/apps-marketplace/v2/reference/,但失败并显示403错误 - 无权访问应用程序ID

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "forbidden",
    "message": "Not authorized to access the application ID"
   }
  ],
  "code": 403,
  "message": "Not authorized to access the application ID"
 }
}

我甚至尝试过创建服务帐户并使用服务帐户访问API,但却遇到了同样的错误。

任何输入都会很棒。

到目前为止,这是我的Java(Groovy技术)代码(响应是我上面粘贴的json):

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport
import com.google.api.client.http.GenericUrl
import com.google.api.client.http.HttpRequest
import com.google.api.client.http.HttpRequestFactory
import com.google.api.client.http.HttpTransport
import com.google.api.client.json.JsonFactory
import com.google.api.client.json.jackson2.JacksonFactory
import org.springframework.security.access.annotation.Secured

class DataController {

    /**
     * Be sure to specify the name of your application. If the application name is {@code null} or
     * blank, the application will log a warning. Suggested format is "MyCompany-ProductName/1.0".
     */
    private static final String APPLICATION_NAME = "My app name";

    private static final String APPLICATION_ID = "12 digit project number";

    /** E-mail address of the service account. */
    private static final String SERVICE_ACCOUNT_EMAIL = "12digitproejectnumber-compute@developer.gserviceaccount.com";

    /** Global instance of the HTTP transport. */
    private static HttpTransport httpTransport;

    /** Global instance of the JSON factory. */
    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();




    def getLicensedInfo() {

        try {
            try {
                httpTransport = GoogleNetHttpTransport.newTrustedTransport();



                GoogleCredential credential = new GoogleCredential.Builder().setTransport(httpTransport)
                        .setJsonFactory(JSON_FACTORY)
                        .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
                        .setServiceAccountScopes(Collections.singleton("https://www.googleapis.com/auth/appsmarketplace.license"))
                        .setServiceAccountPrivateKeyFromP12File(new File("a/valid/path/to/key.p12"))
                        .build();


                credential.refreshToken();
                String token = credential.getAccessToken();

                HttpRequestFactory requestFactory = httpTransport.createRequestFactory(credential);


                GenericUrl url = new GenericUrl("https://www.googleapis.com/appsmarket/v2/licenseNotification/"+APPLICATION_ID);

                HttpRequest request = requestFactory.buildGetRequest(url);

                com.google.api.client.http.HttpResponse response = request.execute();

                log.debug(response.parseAsString());


                return;
            } catch (IOException e) {
                System.err.println(e.getMessage());
            }
        } catch (Throwable t) {
            t.printStackTrace();
        }

    }



}

1 个答案:

答案 0 :(得分:1)

在向GAM许可API发出请求时,您需要使用您用于商家信息的相同Google Developers Console项目。对于加载项,这是与脚本关联的控制台项目。