400错误请求"错误" :" invalid_grant"同时在Android中创建google plus圈子

时间:2015-02-26 13:08:39

标签: java android google-admin-settings-api google-plus-domains

我想在我的Android应用中使用Google plus创建圈子,为此我使用Google plus域API然后进行API身份验证我遵循这些步骤并配置此处描述的所有内容:{{3} }

但是当我要创建这里给出的圆圈时 https://developers.google.com/+/domains/quickstart/java我收到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();
            }      
 }

在我的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)

0 个答案:

没有答案