Google Domain API会在回复中不断返回401/403。我究竟做错了什么?

时间:2017-02-11 02:04:51

标签: java google-plus google-domain-api

我尝试使用Google Domains API将简单的活动插入授权用户的Google+个人资料中。该API似乎仅适用于Google Apps(现在是G Suite)帐户和ISV,因此我创建了一个G Suite帐户。在使用常规Google开发者帐户进行多次尝试后,我在此处显示相同的错误。

理想情况下,我希望通过HTTP端点实现此目标,如文档here所示。

我创建了一个Google应用并生成了OAuth2 CLIENT_ID和CLIENT_SECRET,我当然启用了Domains API。 以下Java代码显示了实现目标所采取的步骤。

import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;

public class GooglePlustShenanigans {

    private static final String CLIENT_SECRET = "<client_secret>";

    private static final String CLIENT_ID = "<client_id>";

    private static final List<String> SCOPE = Arrays.asList(
            "https://www.googleapis.com/auth/plus.me",
            "https://www.googleapis.com/auth/plus.stream.write");
    private static final String REDIRECT_URI = "https://www.someuri.com/oauth2callback";


    public static void main(String[] args) throws IOException {
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                new NetHttpTransport(),
                new JacksonFactory(),
                CLIENT_ID,
                CLIENT_SECRET,
                SCOPE)
                .setAccessType("offline")// Set to offline so that the token can be refreshed.
                .build();
        String url = flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI).build();

        System.out.println("Please open the following URL in your browser then " +
                "type the authorization code:");
        System.out.println("  " + url);

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String code = br.readLine();

        GoogleTokenResponse tokenResponse = flow.newTokenRequest(code)
                .setRedirectUri(REDIRECT_URI).execute();

        String token = tokenResponse.getAccessToken();
        String refreshToken = tokenResponse.getRefreshToken();

        System.out.println("Access token: " + token);
        System.out.println("Refresh token: " + refreshToken);

        post("Sample message", token);
    }

    public static void post(String msg, String token) throws IOException {
        HttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost("https://www.googleapis.com/plusDomains/v1/people/me/activities");
        StringEntity body = new StringEntity("{\"object\":{\"originalContent\":\"" + msg + "\"}," +
                "\"access\":{\"items\":[{\"type\":\"domain\"}],\"domainRestricted\":true}}");
        post.setHeader("Content-Type", "application/json");
        post.setHeader("Authorization", "OAuth" + token);
        post.setEntity(body);

        HttpResponse resp = client.execute(post);
        System.out.println(resp.getStatusLine().getStatusCode());
        System.out.println(resp.getStatusLine().getReasonPhrase());
        System.out.println(resp.toString());
    }
}

以下是我不断得到的回应:

401
Unauthorized
HttpResponseProxy{HTTP/1.1 401 Unauthorized [Vary: Origin, Vary: X-Origin, WWW-Authenticate: Bearer realm="https://accounts.google.com/", Content-Type: application/json; charset=UTF-8, Date: Sat, 11 Feb 2017 00:38:07 GMT, Expires: Sat, 11 Feb 2017 00:38:07 GMT, Cache-Control: private, max-age=0, X-Content-Type-Options: nosniff, X-Frame-Options: SAMEORIGIN, X-XSS-Protection: 1; mode=block, Server: GSE, Alt-Svc: quic=":443"; ma=2592000; v="35,34", Transfer-Encoding: chunked] org.apache.http.client.entity.DecompressingEntity@1649b0e6}

我的下一次尝试是尝试利用Google Java库并最终得到以下结果:

GoogleCredential credential = new GoogleCredential.Builder()
                .setTransport(new NetHttpTransport())
                .setJsonFactory(new JacksonFactory())
                .setClientSecrets(CLIENT_ID, CLIENT_SECRET)
                .build();

        credential.setFromTokenResponse(tokenResponse);
        credential.refreshToken();    
String msg = "Happy Monday! #caseofthemondays";

    // Create a list of ACL entries
    PlusDomainsAclentryResource resource = new PlusDomainsAclentryResource();
    resource.setType("domain"); // Share to domain

    List<PlusDomainsAclentryResource> aclEntries =
            new ArrayList<>();
    aclEntries.add(resource);

    Acl acl = new Acl();
    acl.setItems(aclEntries);
    acl.setDomainRestricted(true);  // Required, this does the domain restriction

    PlusDomains plusDomains = new PlusDomains
            .Builder(new NetHttpTransport(), new JacksonFactory(), credential)
            .setApplicationName("myApp")
            .build();

    Activity activity = new Activity()
            .setObject(new Activity.PlusDomainsObject().setOriginalContent(msg))
            .setAccess(acl);
    activity = plusDomains.activities().insert("me", activity).execute();

    System.out.println(activity.toPrettyString());

输出:

    Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 403 Forbidden
{
  "code" : 403,
  "errors" : [ {
    "domain" : "global",
    "message" : "Forbidden",
    "reason" : "forbidden"
  } ],
  "message" : "Forbidden"
}
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1065)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)

当我尝试通过API Explorer实现此目的时,我收到了同样的403错误。

然后我没有特别的理由尝试过:

curl -v -H "Content-Type: application/json" -H "Authorization: OAuth$USER_ACCESS_TOKEN" -d "{"object": {"originalContent": "Happy Monday!#caseofthemondays"},"access":{"kind":"plus#acl","items":[{"type":"domain"}],"domainRestricted":true}}" -X POST https://www.googleapis.com/plusDomains/v1/people/me/activities

响应:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "authError",
    "message": "Invalid Credentials",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Invalid Credentials"
 }
}

我错过了什么?

更新:我尝试为经过身份验证的G Suite域用户创建帖子,但也遇到了相同的错误。

1 个答案:

答案 0 :(得分:1)

要使用google plus域api,您需要创建帐户http://apps.google.com/,然后使用此用户创建帖子。但!!!他们创建帖子域名限制。我现在还没有找到如何创建公共帖子的方法。

请参阅此链接了解更多信息......

Google Plus Creating a Post - Rest API -Forbidden

How to create a new post on Google+ via javascript API?

How to post in Google+ wall

希望它可以帮助你...

相关问题