繁体   English   中英

尝试从Java使用oAuth2访问Picasaweb API时出现错误“无效令牌:无法解析引用的令牌字符串”

[英]Error “Invalid token: Cannot parse referred token string” when trying to access to Picasaweb API with oAuth2 from Java

尝试以Java连接到Picasaweb API时,出现以下错误:

com.google.gdata.util.ServiceForbiddenException: Forbidden
Token invalid - Invalid token: Cannot parse referred token string
    at com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:605)
    at com.google.gdata.client.http.GoogleGDataRequest.handleErrorResponse(GoogleGDataRequest.java:564)
    at com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:560)
    at com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:538)
    at com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:536)
    at com.google.gdata.client.Service.insert(Service.java:1409)
    at com.google.gdata.client.GoogleService.insert(GoogleService.java:613)
    at com.google.gdata.client.media.MediaService.insert(MediaService.java:380)

这是我的代码:

final String[] SCOPESArray = { "https://picasaweb.google.com/data/" };
final List SCOPES = Arrays.asList(SCOPESArray);
final GoogleCredential credential = new GoogleCredential.Builder()
  .setTransport(new NetHttpTransport())
  .setJsonFactory(new JacksonFactory())
  .setServiceAccountId("xxx@developer.gserviceaccount.com")
  .setServiceAccountScopes(SCOPES)
  .setServiceAccountPrivateKeyFromP12File(new File("key.p12")).build();
final PicasawebService picasaService = new PicasawebService("toto");
picasaService.setOAuth2Credentials(credential);
picasaService.insert(new URL(FEED_URL), album);

我认为缺少somethig,如何在服务帐户和Picasa帐户之间建立链接? 我试图在范围内添加网址,例如https://picasaweb.google.com/data/entry/api/user/ “ + USER_ID或” https://picasaweb.google.com/data/feed/api/user / “ + USER_ID,但没有成功...

每当应用程序访问Picasa相册时,我都希望避免出现“用户同意”对话框。

我没有找到PicasaWeb API和oAuth2的示例,所以如果有人有主意...谢谢!

编辑:我找到了有关“将域范围的权限委派给服务帐户”的信息, https://developers.google.com/identity/protocols/OAuth2ServiceAccount可以通过setServiceAccountUser("user@example.com")来完成我想做的事情似乎仅适用于Google Apps域。

服务帐户和Picasa帐户之间如何建立链接?

没有链接。 就Google而言,服务帐户和Picasa帐户是两个不同的用户。 您可能(由于您没有解释用例,所以我想)不想使用服务帐户。

将Picasa API和OAuth视为两个完全独立的学习练习。 将OAuth视为导致您拥有访问令牌的事物,然后将Picasa API视为将访问令牌戳入的事物。 尝试使用OAuth Playground( https://developers.google.com/oauthplayground/ )模拟您想要实现的目标。 这将教会您所有有关OAuth的知识,并向您展示http请求的外观,然后可以将其与您的应用程序进行比较。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM