0

我想从我的 android 应用程序中在 Google plus 中创建圈子,为此我正在使用 Google plus 域 API,然后对于 API 身份验证,我遵循这些步骤并配置此处描述的所有内容:https ://developers.google.com ///域/快速入门/java

但是,当我要创建这里给出的圈子 https://developers.google.com/+/domains/circles/creating我得到 400 Bad request "error": "invalid_grant" 那么它似乎是 authonticating 但不是创建圈子。

有谁能够帮我 ?感谢考虑...

我的代码是:

在我的活动中

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        domainDelegation = new DomainDelegation(this);
        PlusDomains service = null;
            try {
                service = domainDelegation.authenticate();
                People people = service.people();
                domainDelegation.createCircle(service);
            } catch (GeneralSecurityException | IOException e) {
                e.printStackTrace();
            }      
 }

在 My DomainDelegation 类中

private static final String SERVICE_ACCOUNT_EMAIL =     "xxxxxxxxx@developer.gserviceaccount.com"; 
private static final String USER_EMAIL = "name@domainname.com";
private static final List<String> SCOPE = Arrays.asList(
        "https://www.googleapis.com/auth/plus.me",
        "https://www.googleapis.com/auth/plus.stream.read",
        "https://www.googleapis.com/auth/plus.profiles.read",
        "https://www.googleapis.com/auth/plus.stream.write",
        "https://www.googleapis.com/auth/plus.circles.read",
        "https://www.googleapis.com/auth/plus.circles.write");

public  PlusDomains authenticate() throws GeneralSecurityException,
        IOException {  
    System.out.println(String.format("Authenticate the domain for %s",
            USER_EMAIL));
    Log.i(TAG, "Authenticate the domain for " + USER_EMAIL);

    HttpTransport httpTransport = new NetHttpTransport();
    JsonFactory jsonFactory = new JacksonFactory();

    // Setting the sub field with USER_EMAIL allows you to make API calls
    // using the special keyword
    // 'me' in place of a user id for that user.
    GoogleCredential credential = new GoogleCredential.Builder()
            .setTransport(httpTransport)
            .setJsonFactory(jsonFactory)
            .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
            .setServiceAccountScopes(SCOPE)
            .setServiceAccountUser(USER_EMAIL)
             // I'm accessing .p12 file from my sdcard 
            .setServiceAccountPrivateKeyFromP12File(
                    new File(getReceiveFilename()))         
                            .build(); 

    // Create and return the Plus service object
    PlusDomains service = new PlusDomains.Builder(httpTransport,
            jsonFactory, credential).setApplicationName("GooglePlusDomainAPI").build();
    return service;
}
public void createCircle(PlusDomains service) {
    Circle circle = new Circle();
    circle.setDisplayName("Tech support");
    Circle result;
    try {
        result = service.circles().insert("me", circle).execute();
        System.out.println("Created 'Tech support' circle with id: " + result.getId());
        Log.i(TAG, "circle with id: "+ result.getId());
    } catch (IOException e) {
        e.printStackTrace();
        Log.v(TAG, "IOException: "+ e.getMessage());
    }
}

获得异常

com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{
"error" : "invalid_grant"
}
at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)
at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)
at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:384)
at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489)
at com.google.api.client.auth.oauth2.Credential.intercept(Credential.java:217)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:859)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
at com.example.googleplusdomainapi.DomainDelegation.createCircle(DomainDelegation.java:144)
at com.example.googleplusdomainapi.MainActivity.doThis(MainActivity.java:95)
at com.example.googleplusdomainapi.MainActivity.access$0(MainActivity.java:57)
at com.example.googleplusdomainapi.MainActivity$1$1.doInBackground(MainActivity.java:43)
at com.example.googleplusdomainapi.MainActivity$1$1.doInBackground(MainActivity.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
4

0 回答 0