আগের সাইন-ইন যোগ করুন পদ্ধতি অনুযায়ী, আপনার অ্যাপ শুধুমাত্র ক্লায়েন্ট সাইডে ব্যবহারকারীকে যাচাই করে; সেই ক্ষেত্রে, ব্যবহারকারী আপনার অ্যাপ সক্রিয়ভাবে ব্যবহার করার সময় আপনি শুধুমাত্র Google API অ্যাক্সেস করতে পারবেন। আপনি যদি চান যে আপনার সার্ভার ব্যবহারকারীর হয়ে Google API কল করুক—সম্ভবত যখন তারা অফলাইনে থাকে—আপনার সার্ভারের একটি অ্যাক্সেস টোকেন প্রয়োজন।
শুরু করার আগে
- Android Studio-তে প্রোজেক্ট কনফিগার করা
- আপনার অ্যাপে Google Sign-In বোতাম যোগ করুন
- আপনার ব্যাকএন্ড সার্ভারের জন্য OAuth 2.0 ওয়েব অ্যাপ্লিকেশন ক্লায়েন্ট আইডি তৈরি করুন। এই ক্লায়েন্ট আইডি আপনার অ্যাপের ক্লায়েন্ট আইডির থেকে আলাদা। আপনি Google API Console থেকে আপনার সার্ভারের জন্য ক্লায়েন্ট আইডি খুঁজে পেতে বা তৈরি করতে পারবেন।
আপনার অ্যাপের জন্য সার্ভার-সাইড API অ্যাক্সেস চালু করুন
আপনি যখন Google Sign-In কনফিগার করবেন,
requestServerAuthCodeপদ্ধতি ব্যবহার করেGoogleSignInOptionsঅবজেক্ট তৈরি করুন এবং আপনার অ্যাপের ব্যাকএন্ডের অ্যাক্সেস করার জন্য প্রয়োজনীয় স্কোপrequestScopesপদ্ধতি ব্যবহার করে নির্দিষ্ট করুন।requestServerAuthCodeপদ্ধতিতে আপনার সার্ভারের ক্লায়েন্ট আইডি পাস করুন।// Configure sign-in to request offline access to the user's ID, basic // profile, and Google Drive. The first time you request a code you will // be able to exchange it for an access token and refresh token, which // you should store. In subsequent calls, the code will only result in // an access token. By asking for profile access (through // DEFAULT_SIGN_IN) you will also get an ID Token as a result of the // code exchange. String serverClientId = getString(R.string.server_client_id); GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestScopes(new Scope(Scopes.DRIVE_APPFOLDER)) .requestServerAuthCode(serverClientId) .requestEmail() .build();
ব্যবহারকারী সাইন-ইন করার পরে,
getServerAuthCode-এর মাধ্যমে ব্যবহারকারীর জন্য একটি অথেন্টিকেশন কোড পান:Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data); try { GoogleSignInAccount account = task.getResult(ApiException.class); String authCode = account.getServerAuthCode(); // Show signed-un UI updateUI(account); // TODO(developer): send code to server and exchange for access/refresh/ID tokens } catch (ApiException e) { Log.w(TAG, "Sign-in failed", e); updateUI(null); }HTTPS POST ব্যবহার করে আপনার অ্যাপের ব্যাকএন্ডে যাচাইকরণ কোড পাঠান:
HttpPost httpPost = new HttpPost("https://yourbackend.example.com/authcode"); try { List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); nameValuePairs.add(new BasicNameValuePair("authCode", authCode)); httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpClient.execute(httpPost); int statusCode = response.getStatusLine().getStatusCode(); final String responseBody = EntityUtils.toString(response.getEntity()); } catch (ClientProtocolException e) { Log.e(TAG, "Error sending auth code to backend.", e); } catch (IOException e) { Log.e(TAG, "Error sending auth code to backend.", e); }আপনার অ্যাপের ব্যাকএন্ড সার্ভারে, অ্যাক্সেস ও রিফ্রেশ টোকেনের জন্য অথ কোড এক্সচেঞ্জ করুন। ব্যবহারকারীর হয়ে Google API কল করতে অ্যাক্সেস টোকেন ব্যবহার করুন এবং বিকল্প হিসেবে, অ্যাক্সেস টোকেনের মেয়াদ শেষ হয়ে গেলে নতুন অ্যাক্সেস টোকেন পাওয়ার জন্য রিফ্রেশ টোকেন স্টোর করুন।
আপনি প্রোফাইল অ্যাক্সেসের অনুরোধ করলে, আপনি একটি আইডি টোকেনও পাবেন যাতে ব্যবহারকারীর প্রাথমিক প্রোফাইল তথ্য থাকে।
যেমন:
জাভা
// (Receive authCode using HTTPS POST) if (request.getHeader("X-Requested-With") == null) { // Without the `X-Requested-With` header, this request could be forged. Aborts. } // Set path to the Web application client_secret_*.json file you downloaded from the // Google API Console: https://console.cloud.google.com/apis/credentials // You can also find your Web application client ID and client secret from the // console and specify them directly when you create the GoogleAuthorizationCodeTokenRequest // object. String CLIENT_SECRET_FILE = "/path/to/client_secret.json"; // Exchange auth code for access token GoogleClientSecrets clientSecrets = GoogleClientSecrets.load( JacksonFactory.getDefaultInstance(), new FileReader(CLIENT_SECRET_FILE)); GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest( new NetHttpTransport(), JacksonFactory.getDefaultInstance(), "https://oauth2.googleapis.com/token", clientSecrets.getDetails().getClientId(), clientSecrets.getDetails().getClientSecret(), authCode, REDIRECT_URI) // Specify the same redirect URI that you use with your web // app. If you don't have a web version of your app, you can // specify an empty string. .execute(); String accessToken = tokenResponse.getAccessToken(); // Use access token to call API GoogleCredential credential = new GoogleCredential().setAccessToken(accessToken); Drive drive = new Drive.Builder(new NetHttpTransport(), JacksonFactory.getDefaultInstance(), credential) .setApplicationName("Auth Code Exchange Demo") .build(); File file = drive.files().get("appfolder").execute(); // Get profile info from ID token GoogleIdToken idToken = tokenResponse.parseIdToken(); GoogleIdToken.Payload payload = idToken.getPayload(); String userId = payload.getSubject(); // Use this value as a key to identify a user. String email = payload.getEmail(); boolean emailVerified = Boolean.valueOf(payload.getEmailVerified()); String name = (String) payload.get("name"); String pictureUrl = (String) payload.get("picture"); String locale = (String) payload.get("locale"); String familyName = (String) payload.get("family_name"); String givenName = (String) payload.get("given_name");Python
from apiclient import discovery import httplib2 from oauth2client import client # (Receive auth_code by HTTPS POST) # If this request does not have `X-Requested-With` header, this could be a CSRF if not request.headers.get('X-Requested-With'): abort(403) # Set path to the Web application client_secret_*.json file you downloaded from the # Google API Console: https://console.cloud.google.com/apis/credentials CLIENT_SECRET_FILE = '/path/to/client_secret.json' # Exchange auth code for access token, refresh token, and ID token credentials = client.credentials_from_clientsecrets_and_code( CLIENT_SECRET_FILE, ['https://www.googleapis.com/auth/drive.appdata', 'profile', 'email'], auth_code) # Call Google API http_auth = credentials.authorize(httplib2.Http()) drive_service = discovery.build('drive', 'v3', http=http_auth) appfolder = drive_service.files().get(fileId='appfolder').execute() # Get profile info from ID token userid = credentials.id_token['sub'] email = credentials.id_token['email']