FLUTTER GOOGLE API
FLUTTER GOOGLE APIS

Hello guys, Welcome to Proto Coders Point. In this Flutter Article we discuss about Google apis Implement in flutter.

Examples of user-data APIs include Calendar, Gmail, and YouTube. 

Note: The only APIs you should use directly from your Flutter project are those that access user data via Google authentication. APIs that require service accounts should not be used directly from a Flutter application.

Direct use of service account-required APIs from a Flutter application is not advised. This necessitates the use of your application’s insecure shipping service credentials. We advise building an intermediary service in order to leverage these APIs.

Here describe use google api in flutter

Google APIs in flutter

Step 1 : Select the preferred API

The package’s API is listed on googleapis as a distinct Dart library using the name_version format. 

For an example let’s take youtube_v3 as an illustration.

In addition to being the class you must instantiate (see step 3), the Api class also exposes the scopes that stand for the permissions required to utilize the API. You can find the scopes available by looking under the Constants section of the YouTubeApi class. Use the youtubeReadonlyScope when authenticating the user to ask for permission to merely read an end-user’s YouTube data.

Import this library

import 'package:googleapis/youtube/v3.dart';

Step 2 : Turn on the API.

You need a Google project and an account on Google in order to access Google APIs. Additionally, you must turn on the desired API.

Using the console, enable an API for a project:

1) Visit the API Library for the Google Cloud console.

2) Choose the project you want to utilize from the list of projects.

3) Choose the API you want to activate from the API Library. Use the search bar and/or the filters if you need assistance finding the API.

4) Click ENABLE on the API page.


Step 3: Use the necessary scopes to authenticate the user

Users can authenticate with their Google identity by using the google_sign_in package. For each platform you want to support, you must configure sign-in.

Dependencies Within pubspec.yaml

dependencies:
  google_sign_in: 
  googleapis: 
import 'package:google_sign_in/google_sign_in.dart';

The desired permissions are supplied when the GoogleSignIn class is instantiated, as was covered in the previous section.

final _googleSignIn = GoogleSignIn(
  scopes: <String>[YouTubeApi.youtubeReadonlyScope],
);

To enable user authentication , adhere to the directions given by package:google_sign_in.

You must get an authenticated HTTP client after completing your authentication.


Step 4: Obtain an HTTP client that is authorized.

A GoogleSignIn extension method called authenticatedClient is offered by the extension_google_sign_in_as_googleapis_auth package.

import 'package:extension_google_sign_in_as_googleapis_auth/extension_google_sign_in_as_googleapis_auth.dart';

OnCurrentUserChanged can be heard. You can design a client that is authorized if the event value is not null.

var httpClient = (await _googleSignIn.authenticatedClient())!;

When calling Google API classes, this Client instance already has the required credentials.


Step 5: Make the desired API class and use it.

Create the desired API type using the API, then call the methods, like in:

var youTubeApi = YouTubeApi(httpClient);

var favorites = await youTubeApi.playlistItems.list(
  ['snippet'],
  playlistId: 'LL',
);

The principles discussed on this page have a working implementation in the extension_google_sign_in_as_googleapis_auth example. We appreciate you joining us on a flutter journey! I hope you succeeded in finding what you sought.