Googleの認証方法について

このQ&Aのポイント
  • Googleの認証方法について質問します。現在、Linuxで動かすgoogleドライブからの情報取得アプリを作成しているのですが、googleの認証方法で困っています。
  • 現在はWindows環境で開発しており、以下の方法を使用してgoogle認証をしています。しかし、Linuxの環境ではブラウザを介さずに認証をする方法が必要です。
  • 一度のみの認証で済むような方法でgoogleのCredentialを取得したいと考えています。ご教授いただけると助かります。
回答を見る
  • ベストアンサー

googleの認証方法について

お世話になっております。 現在、Linuxで動かすgoogleドライブからの情報取得アプリを作成しているのですが googleの認証方法で困っているため御力添えお願いしたく思います。 現在はWindows環境で開発しており、 以下のようにしてgoogle認証をしているのですが 下記の方法だとはじめに一度ブラウザより認証を許可をする必要があり Linuxの環境で使用するにはそぐわないと考えております。 setUserCreadntialsを使用することも検討したのですが OAuthのような一度のみの認証で済むような認証方法で実現したいため googleのCredentialを取得する際に画面入力を介さないような方法が有りましたら ご教授のほどよろしくお願いします。 参考までに現在は下記のようにCreadntialを取得しております。 Credential credential = null; credential = authorize(); //主処理 if(credential == null) {     throw new Exception("認証失敗。"); } SpreadsheetService service = new SpreadsheetService(APP_NAME); service.setOAuth2Credentials(credential); //認証取得メソッド /** * googleの認証情報を返す。 * @return * @throws Exception */ private static Credential authorize() throws Exception { try { FileDataStoreFactory fdsf = new FileDataStoreFactory(new File(JSON_PATH)); GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( new NetHttpTransport(), new JacksonFactory(), CLIENT_ID, CLIENT_SECRET, Collections.singleton(SCOPE)).setCredentialDataStore( StoredCredential .getDefaultDataStore(fdsf)) .setAccessType("ofline").setApprovalPrompt("auto").build(); return flow.loadCredential(ACCOUNT); } catch(Exception e) { e.printStackTrace(); return null; } } 何卒、ご力添えお願い致します。

  • Java
  • 回答数1
  • ありがとう数0

質問者が選んだベストアンサー

  • ベストアンサー
  • teketon
  • ベストアンサー率65% (141/215)
回答No.1

Service Accountを使う。

参考URL:
https://developers.google.com/accounts/docs/OAuth2#serviceaccount

関連するQ&A

  • Googleアカウント取得で携帯認証をやる求められるとき

    Googleアカウント取得で携帯認証をやる求められるとき Googleアカウントを新規で取得すると、 たまに携帯での認証を求められるときがあると聞きます。 しかし、私の作業環境(firefox or Safari)ではそういう画面は出ません。 どういうときに出現するものなのでしょうか? または回避する方法はありますでしょうか?

  • 逆コンパイルの見方について(再度)

    askaaska`様 コンパイルまでは出来ましたが実行段階で不調です。やみくもにやっているのでご迷惑をお掛けしているのは重々承知しています。恐らくこのプログラムからJSPを実行してWebに表示しているものと考えられます。残念ながら画面が遷移しません。このプログラムだけです、あとは無理やり直しました。邪道とは思いますが方法がありません。 public class TranslationEntryBL { private final String STR_ARRAY[]; private TranslationEntryForm srcform; private TranslationEntryBL() { STR_ARRAY = new String[0]; srcform = null; } public TranslationEntryBL(TranslationEntryForm srcform) { STR_ARRAY = new String[0]; this.srcform = null; this.srcform = srcform; } public GenericApplicationForm init() throws Exception { initCommonInfo(); if(srcform.getParentContentID().equalsIgnoreCase(srcform.getSelectContentID()) && !"scx".equalsIgnoreCase(srcform.getContentType())) { srcform.setFocusElement("entryTitle-1"); } else if(srcform.getEntryContent().length > 0) { srcform.setFocusElement("entryContent"); } else { srcform.setFocusElement(""); } return prepareContentData(); } public GenericApplicationForm refresh() throws Exception { return init(); } public GenericApplicationForm returnList() throws Exception { TranslationListForm nextform = new TranslationListForm(srcform); nextform.setEventName("init"); return nextform; } private GenericApplicationForm prepareContentData() throws Exception { Connection conn; DBResult resultP; DBResult resultT; conn = null; resultP = null; resultT = null; DBResult resultC = null; Object obj; conn = DBUtil.getConnection(srcform.getWebInfoPath()); resultP = daGetParentContent(conn); // kokomade okay if(resultP.getRs().next()) { // kokomade okay obj = returnList(); DBUtil.closeConnection(conn); // kokomade okay return ((GenericApplicationForm) (obj)); // kokodetomaru } setParentContent(resultP); resultC = daGetChildContent(conn); setChildContent(resultC); if(!"refresh".equalsIgnoreCase(srcform.getEventName())) { srcform.setSelectContentID(""); String languages[] = GetConfVal.getConfVal("def_datalanguage", srcform.getBasicInfoDTO()).split(","); srcform.setEntryTitle(new String[languages.length]); srcform.setEntryContent(new String[languages.length]); srcform.setEntryContentOriginalFlag(new String[languages.length]); srcform.setEntryContentLanguage(new String[languages.length]); for(int i = 0; i < languages.length; i++) { srcform.setEntryTitle("", i); srcform.setEntryContent("", i); srcform.setEntryContentOriginalFlag("", i); srcform.setEntryContentLanguage("", i); } } try { srcform.setFocusElement(null); obj = srcform; } catch (Exception exception) { DBUtil.closeConnection(conn); throw exception; } DBUtil.closeConnection(conn); return ((GenericApplicationForm) (obj)); } return ((GenericApplicationForm) (obj));のところで処理が止まります。何かアドバイスを下さい御願いします。尚、止まると判断しているのはBEEP音を入れてどこまで動いたかを確認したものです。 最早、専門家でないと出来ない領域かと思いますが、何卒宜しく御願い致します。

    • ベストアンサー
    • Java
  • JDBCプログラミング

    以下のプログラムは、DBkanri.javaというファイル名で、データベースに接続してデータを取得し、取得したデータを表示させるJSPファイルとともに使います。これを、テーブルの作成、レコード追加、フィールド 追加等の更新をし、更新後のデータも取得できるようなプログラムにアレンジしたいのですが、 どなたか、アドバイスください。お願いします。 ちなみに環境は、tomcat3.2.1 JDK1.3.1_02MS Access2000です。 package Kadai; import java.sql.*; public class DBkanri{ private String driver; private String url; private String user; private String password; private Connection connection; private Statement statement; private ResultSet resultset; public DBkanri(){ driver="sun.jdbc.odbc.JdbcOdbcDriver"; url="jdbc:odbc:CONNECTION"; user="abcde"; password="12345"; } public synchronized void open() throws Exception { Class.forName(driver); connection=DriverManager.getConnection(url, user, password); statement=connection.createStatement(); } public ResultSet getResultSet(String sql) throws Exception { if ( statement.execute(sql) ){ return statement.getResultSet(); } return null; } public void execute(String sql) throws Exception { statement.execute(sql); } public synchronized void close() throws Exception { if( resultset != null ) resultset.close(); if( statement != null ) statement.close(); if( connection != null ) connection.close(); } }

  • PHP+OAuthで認証エラー

    OAuth認証で、登録したAPIの認証「○○ががあなたのアカウントを利用することを許可しますか?」で ユーザ/PWを入力してログイン後、oauthライブラリを使用してOAuthオブジェクトを下記の様に作成したのですが。 $connection = new TwitterOAuth('CONSUMER_KEYの値', 'CONSUMER_SECRETの値'); $content = $connection->get('account/verify_credentials'); $connectionのhttp_codeリターンコードが'200'ではなくNULLになってしまい。 更に、$contentの内容に、"Could not authenticate you."という認証エラーが返ってきてしまいユーザ情報が取得できずにいます。 なぜ、認証エラーになってしまうのかが全く見当つかず。。 何か良い検証方法または、アドバイスありましたら教えてください。 参考サイトは http://www.sdn-project.net/labo/oauth.html http://blog.n-colo.net/archives/710 ちなみに、ライブラリのバージョンは0.2.0になります。

    • ベストアンサー
    • PHP
  • javaでクッキーを取得したい

    以下ソースで、ホームページのデータは取得できるのですが、クッキーが必要なページではうまく作動しません。 どのように改良すればよいでしょうか? また、こういったプログラムを作るのに参考になる書籍をご存知でしたら教えてください。 よろしくお願いいたします。 import java.io.*; import java.net.*; class test { public static void main(String[] args) throws Exception { URL url = null; BufferedReader in = null; String readString = null; url = new URL("http://www.yahoo.co.jp"); in = new BufferedReader(new InputStreamReader(url.openStream(),"JISAutoDetect")); while((readString=in.readLine())!=null) System.out.println(readString); in.close(); } }

    • ベストアンサー
    • Java
  • Java での グローバルIP取得

    Java を利用してグローバルIPの取得方法を探しています。 以下のサイトを参考にいたしました。 http://stackoverflow.com/questions/2939218/getting-the-external-ip-address-in-java 文中中ほどの以下のソースを試したのですが「checkip.amazonaws.com」と通信を行うせいか、ワンテンポページ表示に時間がかかります。 JavaでのグローバルIP取得するには他に良い方法はあるのでしょうか? それとも、whatismyipのようなサイトと通信を行わないとできないのでしょうか? よろしくお願い致します。 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; public class IpChecker { public static String getIp() throws Exception { URL whatismyip = new URL("http://checkip.amazonaws.com"); BufferedReader in = null; try { in = new BufferedReader(new InputStreamReader( whatismyip.openStream())); String ip = in.readLine(); return ip; } finally { if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } } }

  • Xercesを使ったjavaでのXML解析

    DOMを使ってXML文書を解析するJavaのソースコードで、DOMパーサは、クラス org.apache.xerces.parsers.DOMParserで参照している下記のプログラムで、 [Fatal Error] :17:109: The entity name must immediately follow the '&' in the entity reference. org.xml.sax.SAXParseException; lineNumber: 17; columnNumber: 109; The entity name must immediately follow the '&' in the entity reference. のエラーが出てしまって、解決策が分かりかねています。Javaのネットワークプログラミングに詳しい方、御教示願えればと思います。 package nikkei; import java.io.ByteArrayInputStream; import org.apache.xerces.parsers.DOMParser; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.InputSource; public class TwitterSearch { public static void main(String[] args) throws Exception { TwitterSearch search = new TwitterSearch(); search.search("日経ソフトウエア"); } public void search(String keyword) throws Exception { SearchAPIClient client = new SearchAPIClient(); String xml = client.execute(keyword); parse(xml); } private void parse(String xml) throws Exception { DOMParser parser = new DOMParser(); try { parser.parse(new InputSource(new ByteArrayInputStream(xml.getBytes()))); Document doc = parser.getDocument(); NodeList entries = doc.getElementsByTagName("entry"); for (int i = 0; i < entries.getLength(); i++) { String name = null; String tweet = null; Element entry = (Element) entries.item(i); NodeList titleList = entry.getElementsByTagName("title"); if (titleList.getLength() == 1) { tweet = titleList.item(0).getTextContent(); } NodeList authorList = entry.getElementsByTagName("author"); if (authorList.getLength() == 1) { Element author = (Element) authorList.item(0); NodeList nameList = author.getElementsByTagName("name"); if (nameList.getLength() == 1) { name = nameList.item(0).getTextContent(); } } System.out.println(name + "さんのツイート"); System.out.println("\t" + tweet); } } catch (Exception e) { e.printStackTrace(); } } } package nikkei; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; public class SearchAPIClient { public String execute(String keyword) throws Exception { String url = "https://twitter.com/search?q=" + keyword; HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); HttpResponse response = httpClient.execute(httpGet); HttpEntity entity = response.getEntity(); if (entity != null) { return EntityUtils.toString(entity); } else { return null; } } } よろしくお願いいたします。

    • ベストアンサー
    • Java
  • javaのエンコードについて?

    下記のプログラムでホームページの情報は読み取ることができるようになったのですが、一部ホームページ(UTF-8エンコードのホームページと思われる)、文字化けを起こして見ることができません。 解決する方法を、教えていただけないでしょうか? おねがいいたします。 import java.io.*; import java.net.*; class test{ public static void main(String[] args) throws Exception { URL url = null; BufferedReader in = null; String readString = null; url = new URL("http://www.faireal.net/"); in = new BufferedReader(new InputStreamReader(url.openStream(),"JISAutoDetect")); while((readString=in.readLine())!=null) System.out.println(readString); in.close(); } }

    • ベストアンサー
    • Java
  • バイト配列 unicode

    java初心者です。 以下のソースコードの 「new String(strVal.getBytes("ISO-8859-1"),"JISAutoDetect」 の部分の意味が分からなくこまっています。 <%@ page import="java.io.*" %> <%! public String strEncode(String strVal) throws UnsupportedEncodeingExcception{ if(strVal==null){ return null; }else{ return new String(strVal.getBytes("ISO-8859-1"),"JISAutoDetect")); } } %> そもそも 「バイト配列とは何か」 「なんで必要なのか?」 など、わかりやすく説明していただけると幸いです。

    • ベストアンサー
    • Java
  • javaのエンコードについて?

    さまざまなホームページを探して、下記のプログラムでホームページの情報は読み取ることができるようになったのですが、文字化けを起こして見ることができません。 何をどうすれば文字化けを防げるのかまったくわかりません。 ご存知の方、おしえていただけないでしょうか? おねがいいたします。 import java.io.*; import java.net.*; class test{ public static void main(String[] args) throws Exception { URL url = null; BufferedReader in = null; String readString = null; url = new URL("http://www.yahoo.co.jp/"); in = new BufferedReader(new InputStreamReader(url.openStream())); while((readString=in.readLine())!=null) System.out.println(readString); in.close(); } }

    • ベストアンサー
    • Java

専門家に質問してみよう