2016-11-01 3 views
0

私はLinux OSベースのカスタム組み込みシステムを使用しています。私のシステムにカメラが接続されているので、私は自分のカメラから撮影したレコードのためにGoogleドライブをクラウドストレージとして使いたいと思っています。私はGoogleドライブを取得するプログラムでアクセスするAPK access_token

https://developers.google.com/drive/v3/web/about-sdk

RESTfulなAPI関数でグーグルドライブAPIの残りのマニュアルを参照して、以下のことにより、ほぼすべてのGoogleドライブAPI関数正常に使用そのために

は、HTTPリクエストでのOAuth2プロトコルに従って認証目的のためにaccess_tokenはを必要とします。このトークンを取得するには、一度の手動操作が必要です。

1-アカウントでGoogleにログインしたときに、Google開発者用コンソールを使用してclient_idとclient_secureを作成する必要がありました。

2次に、これらの資格情報を使用してaccess_codeを取得します。 httpリクエストへの応答で、Googleは私にURLと許可コードを送信します。

3次に、ブラウザを使用してURLにアクセスし、許可コードを入力し、手動で認証を許可します。次に、別のHTTPリクエストでaccess_tokenとrefresh_tokenを取得します。

4-その後、指定されたaccess_tokenでapi関数を正常に使用できます。 (refresh_tokenを使用して期限切れになるとリフレッシュします)

トークンを取得するのは1回の手動操作です。私は自分のコンピュータでこれらの操作を行っているので、組み込みシステムでそれらを動作させることは非常に困難です。しかし、私は私の組み込みシステムですべてのステップ(サインイン、client_idとclient_secure、URLの訪問、許可コードの入力と認証の許可)を行いたいと思います。

開発者向けのコンソールとブラウザを使用せずにaccess_codeを取得することができますか?プログラムで作ることはできますか?


これは、私がoauth2 Googleの公式ドキュメントに従ったものです。

client_idとclient_Secretを作成した後、私は単純にhttp操作にcurlを使用しました。私はテスト目的のために次のbashコードを書いた。したがって、自信を持っておらず、最初は空であり、同じディレクトリ内の "access_token"、 "folder_id"、 "refresh_token"、 "myFile"という名前のファイルが必要です。

#!/bin/bash 

# Google Drive API 

get_file_id() { 
    RESPONSE=`curl --silent -H 'GData-Version: 3.0' -H "Authorization: Bearer $ACCESS_TOKEN" \ 
     https://www.googleapis.com/drive/v2/files?q=title+contains+\'$1\'\&fields=items%2Fid` 
    FILE_ID=`echo $RESPONSE | python -mjson.tool | grep -oP 'id"\s*:\s*"\K(.*)"' | sed 's/"//'` 
} 

set -e 

CLIENT_SECRET="my client_secret" 
CLIENT_ID="my_client_id" 
BOUNDARY="foo_bar_baz" 
SCOPE="https://docs.google.com/feeds" 
MIME_TYPE="application/octet-stream" 
ACCESS_TOKEN=`cat access_token` 
REFRESH_TOKEN=`cat refresh_token` 
FOLDER_ID=`cat folder_id` 

if [ "$1" == "create_token" ]; then # Usage: <"create_token"> 
    RESPONSE=`curl --silent "https://accounts.google.com/o/oauth2/device/code" -d "client_id=$CLIENT_ID&scope=$SCOPE"` 
    DEVICE_CODE=`echo "$RESPONSE" | python -mjson.tool | grep -oP 'device_code"\s*:\s*"\K(.*)"' | sed 's/"//'` 
    USER_CODE=`echo "$RESPONSE" | python -mjson.tool | grep -oP 'user_code"\s*:\s*"\K(.*)"' | sed 's/"//'` 
    URL=`echo "$RESPONSE" | python -mjson.tool | grep -oP 'verification_url"\s*:\s*"\K(.*)"' | sed 's/"//'` 
    echo -n "Go to $URL and enter $USER_CODE to grant access to this application. Hit enter when done..." 
    read 

    RESPONSE=`curl --silent "https://accounts.google.com/o/oauth2/token" -d "client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET&code=$DEVICE_CODE&grant_type=http://oauth.net/grant_type/device/1.0"` 
    ACCESS_TOKEN=`echo "$RESPONSE" | python -mjson.tool | grep -oP 'access_token"\s*:\s*"\K(.*)"' | sed 's/"//'` 
    REFRESH_TOKEN=`echo "$RESPONSE" | python -mjson.tool | grep -oP 'refresh_token"\s*:\s*"\K(.*)"' | sed 's/"//'` 
    echo "Access Token: $ACCESS_TOKEN" 
    echo "Refresh Token: $REFRESH_TOKEN" 
    echo "$ACCESS_TOKEN" > access_token 
    echo "$REFRESH_TOKEN" > refresh_token 

elif [ "$1" == "refresh_token" ]; then # Usage: <"refresh_token"> 
    RESPONSE=`curl --silent "https://accounts.google.com/o/oauth2/token" --data "client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET&refresh_token=$REFRESH_TOKEN&grant_type=refresh_token"` 
    ACCESS_TOKEN=`echo $RESPONSE | python -mjson.tool | grep -oP 'access_token"\s*:\s*"\K(.*)"' | sed 's/"//'` 
    echo "Access Token: $ACCESS_TOKEN" 
    echo "$ACCESS_TOKEN" > access_token 

elif [ "$1" == "create_folder" ]; then # Usage: <"create_folder"> 
    FOLDER_NAME=`date "+%F-%T"` 
    (echo -en "{ \"title\": \"$FOLDER_NAME\", \"mimeType\": \"application/vnd.google-apps.folder\" }\n") \ 
     | curl -H 'GData-Version: 3.0' -v "https://www.googleapis.com/drive/v2/files" \ 
     --header "Authorization: Bearer $ACCESS_TOKEN" \ 
     --header "Content-Type: application/json" \ 
     --data-binary "@-" 
    #save FILE_ID to filde 
    get_file_id $FOLDER_NAME 
    echo "$FILE_ID" > folder_id 

elif [ "$1" == "upload_file" ]; then # Usage: <"upload_file"> <file name> 
    (echo -en "--$BOUNDARY\nContent-Type: application/json; charset=UTF-8\n\n{ \"title\": \"$2\", \"parents\": [ { \"id\": \"$FOLDER_ID\" } ] }\n\n--$BOUNDARY\nContent-Type: $MIME_TYPE\n\n" \ 
    && cat $2 && echo -en "\n\n--$BOUNDARY--\n") \ 
     | curl -H 'GData-Version: 3.0' -v "https://www.googleapis.com/upload/drive/v2/files/?uploadType=multipart" \ 
     --header "Authorization: Bearer $ACCESS_TOKEN" \ 
     --header "Content-Type: multipart/related; boundary=\"$BOUNDARY\"" \ 
     --data-binary "@-" 

elif [ "$1" == "list_files" ]; then # Usage: <"list_files"> <number of files> 
    curl -H 'GData-Version: 3.0' -H "Authorization: Bearer $ACCESS_TOKEN" \ 
     https://www.googleapis.com/drive/v2/files?maxResults=$2 

elif [ "$1" == "download_file" ]; then # Usage: <"download_file"> <file name> 
    get_file_id $2 
    curl -H 'GData-Version: 3.0' -H "Authorization: Bearer $ACCESS_TOKEN" \ 
     https://www.googleapis.com/drive/v2/files/$FILE_ID?alt=media 

elif [ "$1" == "get_file" ]; then # Usage: <"get_file"> <file name> 
    get_file_id $2 
    curl -H 'GData-Version: 3.0' -H "Authorization: Bearer $ACCESS_TOKEN" \ 
     https://www.googleapis.com/drive/v2/files/$FILE_ID 

elif [ "$1" == "delete_file" ]; then # Usage: <"delete_file"> <file name> 
    get_file_id $2 
    curl -X Delete -H 'GData-Version: 3.0' -H "Authorization: Bearer $ACCESS_TOKEN" \ 
     https://www.googleapis.com/drive/v2/files/$FILE_ID 

elif [ "$1" == "trash_file" ]; then # Usage: <"trash_file"> <file name> 
    get_file_id $2 
    curl -d -H 'GData-Version: 3.0' -H "Authorization: Bearer $ACCESS_TOKEN" \ 
     https://www.googleapis.com/drive/v2/files/$FILE_ID/trash 

elif [ "$1" == "untrash_file" ]; then # Usage: <"untrash_file"> <file name> 
    get_file_id $2 
    curl -d -H 'GData-Version: 3.0' -H "Authorization: Bearer $ACCESS_TOKEN" \ 
     https://www.googleapis.com/drive/v2/files/$FILE_ID/untrash 
fi 

exit 0 

よろしくこれを行うには

+0

お手数ですが、この[よくある質問](http://stackoverflow.com/questions/10835365/authenticate-programmatically-to-google-with-oauth2)をご確認ください。 – KENdi

+0

oauth2 googleの公式ドキュメントに記載されています。あなたがそこから試したことを私たちに示してください。 –

+0

client_idとclient_Secretを作成した後、http操作にcurlを使用しました。 –

答えて

0

一つの方法はDomain-Wide Delegation of Authority、サービスアカウントを使用することです。このメソッドは、認証のためにJWTで動作します。このプロセスはここで説明されます:Using OAuth 2.0 for Server to Server Applications

アプリケーションは、ドライブファイルの格納に使用するidとして認証するためにサービスアカウントを使用します。

+0

いいえ、hesの質問はドメイン全体の認証とは無関係です。 –

+0

真ですが、ユーザーとして認証すると、更新トークンを格納することはできますが、サービスアカウントは簡単です。IMHO。私はGスイート環境を想定しましたが、それはそうではないかもしれません。それは、PC上の資格情報を取得し、組み込みシステムに転送することですか? –

+0

組み込みシステムはそれらを直接取得できます。上記のコメントの最初のリンクを参照してください。 –

関連する問題