picasawebservice java身份验证访问被拒绝

时间:2015-10-06 12:57:44

标签: google-oauth picasa

当Google切换到OAuth 2身份验证时,我以前工作过的picasawebservice代码失败了。

我已修改代码以使用我需要设置服务帐户的Goodle凭据。

虽然我将凭证添加到PicasawebService对象时没有返回任何错误,但我无法将任何照片添加到我的相册中。

我使用网址https://picasaweb.google.com/data/feed/api/user/xxx?kind=album获取了相册列表,但它只返回公开的个人资料相册,而不是帐户中的其他相册,当我使用Feed返回的相册ID时,我得到了“我尝试插入照片时出现“禁止访问”错误。 (xxx - 我已使用帐户的用户ID和电子邮件地址,并在每种情况下获得相同的结果。)

我已经能够为帐户上的私人相册建立alubum id,但在尝试插入时使用这些也失败了。

代码是

      String emailAddress = "-- the email address from the Google Developers console for the credential --";
      JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
      GoogleCredential credential = null;
      try {
        HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        String[] SCOPESArray = {"https://picasaweb.google.com/data","http://picasaweb.google.com/data/feed/api"};
        final List<String> SCOPES = Arrays.asList(SCOPESArray);

        credential = new GoogleCredential.Builder()
            .setTransport(httpTransport)
            .setJsonFactory(JSON_FACTORY)
            .setServiceAccountId(emailAddress)
            .setServiceAccountPrivateKeyFromP12File(new File("-- the p12 key file --"))
            .setServiceAccountScopes(SCOPES)
            .build();
      } catch (GeneralSecurityException ex) {
        logger.error("processUpload: Error getting Google credential " + accountBean.getZuzzAccountId() + " - " + accountBean.getAccountTitle() + " - " + ex.toString());
        uploadMessage = "Error getting a connection to the photo store - please see the administratior";
        return;
      }    

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

      PicasawebService myService = new PicasawebService("Zuzz-ZuzzClub-1.2");
      myService.setOAuth2Credentials(credential);

      URL albumPostUrl = null;
      try {
        albumPostUrl = new URL("https://picasaweb.google.com/data/feed/api/user/--  my user id --/albumid/-- album id returned from feed --");
      } catch (MalformedURLException ex) {
        logger.error("processUpload: Error getting album post url for account " + accountBean.getZuzzAccountId() + " - " + accountBean.getAccountTitle() + " - " + ex.toString());
        uploadMessage = "Error getting a connection to the photo store - please see the administratior";
        return;
      }

      PhotoEntry myPhoto = new PhotoEntry();
      myPhoto.setTitle(new PlainTextConstruct(fileName));
      myPhoto.setDescription(new PlainTextConstruct(months.get(Integer.valueOf(editionMonth).intValue()) + " " + editionYear));
      myPhoto.setClient("ZuzzClub");

      MediaStreamSource myMedia = null;
      myMedia = new MediaStreamSource(imageInStream,"image/jpeg");
      if (myMedia == null) {
        logger.error("processUpload: Error generating the thumbnail photo - null media source for account " + accountBean.getZuzzAccountId() + " - " + accountBean.getAccountTitle());
        uploadMessage = "Error generating the thumbnail photo - please see the administratior";
        return;
      }
      myPhoto.setMediaSource(myMedia);

      thumbNailUrl = null;
      try {
        PhotoEntry returnedPhoto = myService.insert(albumPostUrl, myPhoto);
        ArrayList<MediaContent> mediaContent = (ArrayList<MediaContent>) returnedPhoto.getMediaContents();
        thumbNailUrl = mediaContent.get(0).getUrl();
      } catch (IOException ex) {
        logger.error("processUpload: Error adding photo to Photo store for account " + accountBean.getZuzzAccountId() + " - " + accountBean.getAccountTitle() + " - " + ex.toString());
        uploadMessage = "Error generating the thumbnail photo - please see the administratior";
        return;
      } catch (ServiceException ex) {
        logger.error("processUpload: Error adding photo to Photo store for account " + accountBean.getZuzzAccountId() + " - " + accountBean.getAccountTitle() + " - " + ex.toString());
        uploadMessage = "Error generating the thumbnail photo - please see the administratior";
        return;
      } finally {
        imageInStream.close();
      }

问题似乎是身份验证未授予执行插入的权限,但服务帐户在Google Developers Console中设置为“可以编辑”。

有没有办法找出申请凭证后服务的权限?

在身份验证更改后,是否有人设法将照片添加到Picasa相册?

0 个答案:

没有答案