2017-07-10 12 views
0

Javaクイックスタートに行って、ログインしたアカウントに接続しているすべてのユーザーの完全なリストを表示する作業アプリケーションを入手しました。私はJavaのドキュメントに行きましたが、ユーザーの削除に関連する唯一のことはUserクラスの "setDeletionTime"ですが、ダミーのアカウントで試してみました。今日に設定された時間を作成しようとしましたが、ユーザーを削除するために働いていませんでした。私はここで何が欠けているのか分かりません。私が使用していますJava Google Admin SDK APIを使用してユーザーを削除する方法

コードは、そのほとんどが、私はGoogleが、私は最終的にそれを把握提供していたことをこの上で見つけることができるすべてのものを読んだ後、Googleのクイックスタート

import com.google.api.client.auth.oauth2.Credential; 

import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledAp; 

import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; 

import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; 

import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; 

import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 

import com.google.api.client.http.HttpTransport; 
import com.google.api.client.json.jackson2.JacksonFactory; 
import com.google.api.client.json.JsonFactory; 
import com.google.api.client.util.store.FileDataStoreFactory; 
import com.google.api.services.admin.directory.DirectoryScopes; 
import com.google.api.services.admin.directory.model.*; 
import com.google.api.services.admin.directory.Directory; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.Arrays; 
import java.util.List; 


public class Quickstart { 
/** Application name. */ 
private static final String APPLICATION_NAME = 
    "Directory API Java Quickstart"; 

/** Directory to store user credentials for this application. */ 
private static final java.io.File DATA_STORE_DIR = new java.io.File(
    System.getProperty("user.home"), ".credentials/admin-directory_v1-java-quickstart"); 

/** Global instance of the {@link FileDataStoreFactory}. */ 
private static FileDataStoreFactory DATA_STORE_FACTORY; 

/** Global instance of the JSON factory. */ 
private static final JsonFactory JSON_FACTORY = 
    JacksonFactory.getDefaultInstance(); 

/** Global instance of the HTTP transport. */ 
private static HttpTransport HTTP_TRANSPORT; 

/** Global instance of the scopes required by this quickstart. 
* 
* If modifying these scopes, delete your previously saved credentials 
* at ~/.credentials/admin-directory_v1-java-quickstart 
*/ 
private static final List<String> SCOPES = 
    Arrays.asList(DirectoryScopes.ADMIN_DIRECTORY_USER_READONLY); 

static { 
    try { 
     HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); 
     DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR); 
    } catch (Throwable t) { 
     t.printStackTrace(); 
     System.exit(1); 
    } 
} 

/** 
* Creates an authorized Credential object. 
* @return an authorized Credential object. 
* @throws IOException 
*/ 
public static Credential authorize() throws IOException { 
    // Load client secrets. 
    /* This does not work as of now 
    InputStream in = Quickstart.class.getResourceAsStream("src/resources/client_secret.json"); 
    */ 
    InputStream in = new FileInputStream("src/resources/client_secret.json"); 
    GoogleClientSecrets clientSecrets = 
     GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); 

    // Build flow and trigger user authorization request. 
    GoogleAuthorizationCodeFlow flow = 
      new GoogleAuthorizationCodeFlow.Builder(
        HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) 
      .setDataStoreFactory(DATA_STORE_FACTORY) 
      .setAccessType("offline") 
      .build(); 
    Credential credential = new AuthorizationCodeInstalledApp(
     flow, new LocalServerReceiver()).authorize("user"); 
    System.out.println(
      "Credentials saved to " + DATA_STORE_DIR.getAbsolutePath()); 
    return credential; 
} 

/** 
* Build and return an authorized Admin SDK Directory client service. 
* @return an authorized Directory client service 
* @throws IOException 
*/ 
public static Directory getDirectoryService() throws IOException { 
    Credential credential = authorize(); 
    return new Directory.Builder(
      HTTP_TRANSPORT, JSON_FACTORY, credential) 
      .setApplicationName(APPLICATION_NAME) 
      .build(); 
} 

public static void main(String[] args) throws IOException { 


    // Build a new authorized API client service. 
    Directory service = getDirectoryService(); 

    // Print the first 10 users in the domain. 
    Users result = service.users().list().setCustomer("my_customer").setOrderBy("email").execute(); 
    List<User> users = result.getUsers(); 


    if (users == null || users.size() == 0) { 
     System.out.println("No users found."); 
    } else { 
     for (User user : users) { 
      //This is where I tried to delete the users 
      //I have also tried using a normal for loop and nothing changes 
      that 
      System.out.println(); 
     } 
    } 

} 
+0

はあなたが好きな詳細追加することができます:

は、ここで私はあなたにこれの完成版を示すために私のコードを添付します使用されるコードを、エラーの問題が発生しましたか? [良い質問をするにはどうすればいいですか?](http://stackoverflow.com/help/how-to-ask)、[最小限で完全で検証可能なサンプルを作成する方法](http://stackoverflow.com/ help/mcve)あなたが試したことをコミュニティに示します。 – abielita

+0

コードやプログラミングに問題はありません。私はちょうどユーザーを削除する方法を知る必要があります。私はどこにでも見てきましたが、私はそれを見つけることができません。なぜここに頼んでいるのですか? –

答えて

0

からコピーされた....私は思います。私はここでそれを説明し、これが答えとして働くのでそれを持っていきます。しかし、もし私が何か間違っていたら、教えてください。とにかく、ここに私がしたことがあります:

だから最初のものが最初です。すべてのアクション(私が言うことができる限り)はhttpとURLをコマンドで結合しています。これは、何かが起こるためには、アクション/コマンドを保持するHttpRequestオブジェクトを作成するために、トランスポート(HttpTransportクラスによって与えられる)とファクトリ(HttpRequestFactoryクラスによって与えられる)を持たなければならないことを意味します。

HttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); 
HttpRequestFactory HTTP_REQUEST_FACTORY = HTTP_TRANSPORT.createRequestFactory(); 
HttpRequest deleteRequest = HTTP_REQUEST_FACTORY.buildDeleteRequest(new GenericUrl("https://www.googleapis.com/admin/directory/v1/users/userkey")); 

しかし、待って:私たちが作ることになります

要求は、すべてがこのような何かを行うことによって行うことができます

この「ユーザー・アカウントの削除」の下hereを示すDELETEリクエストです!ここでは非常に重要なキー要素が欠落しています。私たちは、ヘッダーに印を付けるための正しい資格情報をFactoryに提供する必要があります。基本的にはGoogleにユーザーを削除できることを伝えることです。それではどうやってやるの?

まず、私たちはアクセスしたい範囲を設定する必要があります。私たちのスコープはADMIN_DIRECTORY_USERです。このような範囲を設定します(あなたは既にこのプログラムを実行している場合は、.credentialsディレクトリ内のファイルを削除していることを確認してください!!!!)私たちは、資格情報オブジェクトを持っている必要があり

List<String> SCOPES = Arrays.asList(DirectoryScopes.ADMIN_DIRECTORY_USER); 

次。これは、Googleがquickstartauthorizeメソッド)で提供する方法を使用して行うことができます。私たちの工場に資格情報を提供するために、我々は単にそれを渡すことで、上記の行を編集した資格情報は、オブジェクト:

HttpRequestFactory HTTP_REQUEST_FACTORY = HTTP_TRANSPORT.createRequestFactory(credentials); 

注:これは、少なくとも私にとっては(nullのようcredentials.getRequestInitializer()方法からそれをHttpRequestInitializerに合格しない、この私はそれをやっているやり方であるかもしれないが、私はむしろそれを試していない)。

import com.google.api.client.auth.oauth2.Credential; 
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; 
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; 
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; 
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; 
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; 
import com.google.api.client.http.GenericUrl; 
import com.google.api.client.http.HttpRequest; 
import com.google.api.client.http.HttpRequestFactory; 
import com.google.api.client.http.HttpTransport; 
import com.google.api.client.json.jackson2.JacksonFactory; 
import com.google.api.client.json.JsonFactory; 
import com.google.api.client.util.store.FileDataStoreFactory; 

import com.google.api.services.admin.directory.DirectoryScopes; 

import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.Arrays; 
import java.util.List; 

public class Quickstart { 
    /** Application name. */ 
    private static final String APPLICATION_NAME = "Deleting user example"; 

    /** Directory to store user credentials for this application. */ 
    private static final java.io.File DATA_STORE_DIR = new java.io.File(System.getProperty("user.home"), ".credentials/admin-directory_v1-java-quickstart"); 

    /** Global instance of the {@link FileDataStoreFactory}. */ 
    private static FileDataStoreFactory DATA_STORE_FACTORY; 

    /** Global instance of the JSON factory. */ 
    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); 

    /** Global instance of the HTTP transport. */ 
    private static HttpTransport HTTP_TRANSPORT; 

    //This creates the factory that is used for the user made requests 
    private static HttpRequestFactory HTTP_REQUEST_FACTORY; 

    //This is the credentials for the entire application 
    private static Credential credential; 

    /** Global instance of the scopes required by this quickstart. 
    * 
    * If modifying these scopes, delete your previously saved credentials 
    * at ~/.credentials/admin-directory_v1-java-quickstart 
    */ 
    private static final List<String> SCOPES = Arrays.asList(DirectoryScopes.ADMIN_DIRECTORY_USER); 

    static { 
     try { 
      HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); 
      DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR); 
     } catch (Throwable t) { 
      t.printStackTrace(); 
      System.exit(1); 
     } 
    } 


    /** 
    * Creates an authorized Credential object. 
    * @return an authorized Credential object. 
    * @throws IOException 
    */ 
    public static Credential authorize() throws IOException { 
     // Load client secrets. 
     /* This does not work as of now 
     InputStream in = Quickstart.class.getResourceAsStream("src/resources/client_secret.json"); 
     */ 
     InputStream in = new FileInputStream("src/resources/client_secret.json"); 
     GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); 

     // Build flow and trigger user authorization request. 
     GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES).setDataStoreFactory(DATA_STORE_FACTORY).setAccessType("offline").build(); 
     Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user"); 
     System.out.println("Credentials saved to " + DATA_STORE_DIR.getAbsolutePath()); 
     return credential; 
    } 


    public static void main(String[] args) throws IOException { 



     System.out.println("Deleting user with email"); 
     credential = authorize(); 
     HTTP_REQUEST_FACTORY = HTTP_TRANSPORT.createRequestFactory(credential); 
     HttpRequest deleteRequest = HTTP_REQUEST_FACTORY.buildDeleteRequest(new GenericUrl("https://www.googleapis.com/admin/directory/v1/users/REPLACEMEWITHEMAILORUSERKEY")); 
     deleteRequest.execute(); 

    } 

} 
関連する問題