<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: PhamVanHaoQuang</title>
    <description>The latest articles on Forem by PhamVanHaoQuang (@phamvanhaoquang).</description>
    <link>https://forem.com/phamvanhaoquang</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F893691%2Fa1260ad7-f8fd-4c30-a4e0-5f3a6ab885f8.png</url>
      <title>Forem: PhamVanHaoQuang</title>
      <link>https://forem.com/phamvanhaoquang</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/phamvanhaoquang"/>
    <language>en</language>
    <item>
      <title>How to add Google Sign In to your flutter app?</title>
      <dc:creator>PhamVanHaoQuang</dc:creator>
      <pubDate>Mon, 18 Jul 2022 08:23:16 +0000</pubDate>
      <link>https://forem.com/phamvanhaoquang/how-to-add-google-sign-in-to-your-flutter-app-ja</link>
      <guid>https://forem.com/phamvanhaoquang/how-to-add-google-sign-in-to-your-flutter-app-ja</guid>
      <description>&lt;h2&gt;
  
  
  Login with google.
&lt;/h2&gt;

&lt;p&gt;Use package &lt;strong&gt;google_sign_in&lt;/strong&gt; on &lt;strong&gt;pub.dev&lt;/strong&gt; with lastest version.&lt;br&gt;
  Create a project Firebase to register your application with Google. Create 2 app for Android and IOS like image below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rqxCjYJ5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/33fpkbsj0w3em3t01u32.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rqxCjYJ5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/33fpkbsj0w3em3t01u32.png" alt="Firebase Image" width="880" height="482"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Add 2 files google_services.json and GoogleService-info.plist to your project:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UTLULsDR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/whvbcfot4huu0a5sfo4i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UTLULsDR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/whvbcfot4huu0a5sfo4i.png" alt="Add google_services.json" width="364" height="476"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mRLhtDtL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7tnyr7sc9dzsd5b1whqb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mRLhtDtL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7tnyr7sc9dzsd5b1whqb.png" alt="Add GoogleService-info.plist" width="313" height="491"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  1 Setup for Android.
&lt;/h3&gt;

&lt;p&gt;You do need to enable the OAuth APIs, using the &lt;a href="https://console.cloud.google.com"&gt;&lt;strong&gt;Google Cloud Platform API manager&lt;/strong&gt;&lt;/a&gt;. After that, you can use API of Google like &lt;a href="https://developers.google.com/people/"&gt;People API&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Open &lt;strong&gt;your_app &amp;gt; android &amp;gt; build.gradle&lt;/strong&gt; add this declare to dependencies block:&lt;br&gt;
&lt;code&gt;classpath 'com.google.gms:google-services:4.3.13'&lt;br&gt;
        compile 'com.google.apis:google-api-services-people:v1-rev20210903-1.32.1'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It's look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; dependencies {
        classpath 'com.android.tools.build:gradle:7.1.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.13'
        compile 'com.google.apis:google-api-services-people:v1-rev20210903-1.32.1'
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After setup, you need one more step to remove Exception. Go to Firebase project you created, open Project Overview, Project settings add copy field Public-facing name.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BSN-P7Fm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wrkdhr3uhvtxjuut7e3p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BSN-P7Fm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wrkdhr3uhvtxjuut7e3p.png" alt="Firebase settings" width="880" height="353"&gt;&lt;/a&gt;&lt;br&gt;
Then, open tab OAuth consent screen in your Google Cloud, filled out all required fields in the console to avoid APIException.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CvntVLYQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zp3b6ai6ice1vk6bpoug.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CvntVLYQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zp3b6ai6ice1vk6bpoug.png" alt="OAuth screen" width="880" height="590"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  2 Setup for IOS
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;When you create IOS app, Make sure download file GoogleService-Info.plist in step 1.&lt;/li&gt;
&lt;li&gt;Open Xcode, then right-click on Runner directory and select Add Files to "Runner".&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--z0gNfYQZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6rfachcdry14xkp5v3hj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--z0gNfYQZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6rfachcdry14xkp5v3hj.png" alt="Add file to Runner" width="694" height="1736"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Select GoogleService-Info.plist from the file manager.&lt;/li&gt;
&lt;li&gt;A dialog will show up and ask you to select the targets, select the Runner target.&lt;/li&gt;
&lt;li&gt;Then add the CFBundleURLTypes attributes below into the [my_project]/ios/Runner/Info.plist file.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!-- Put me in the [my_project]/ios/Runner/Info.plist file --&amp;gt;
&amp;lt;!-- Google Sign-in Section --&amp;gt;
&amp;lt;key&amp;gt;CFBundleURLTypes&amp;lt;/key&amp;gt;
&amp;lt;array&amp;gt;
    &amp;lt;dict&amp;gt;
        &amp;lt;key&amp;gt;CFBundleTypeRole&amp;lt;/key&amp;gt;
        &amp;lt;string&amp;gt;Editor&amp;lt;/string&amp;gt;
        &amp;lt;key&amp;gt;CFBundleURLSchemes&amp;lt;/key&amp;gt;
        &amp;lt;array&amp;gt;
            &amp;lt;!-- TODO Replace this value: --&amp;gt;
            &amp;lt;!-- Copied from GoogleService-Info.plist key REVERSED_CLIENT_ID --&amp;gt;
            &amp;lt;string&amp;gt;REVERSED_CLIENT_ID&amp;lt;/string&amp;gt;
        &amp;lt;/array&amp;gt;
    &amp;lt;/dict&amp;gt;
&amp;lt;/array&amp;gt;
&amp;lt;!-- End of the Google Sign-in Section --&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  3 Use the plugin.
&lt;/h3&gt;

&lt;p&gt;Add the following import to your Dart code:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import 'package:google_sign_in/google_sign_in.dart';&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Initialize GoogleSignIn with the scopes you want:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GoogleSignIn _googleSignIn = GoogleSignIn(
  scopes: [
    'email',
    'https://www.googleapis.com/auth/contacts.readonly',
  ],
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can now use the GoogleSignIn class to authenticate in your Dart code, e.g.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Sign in with Google
Future&amp;lt;void&amp;gt; _handleSignIn() async {
    try {
      await _googleSignIn.signIn();
    } catch (error) {
      if (kDebugMode) {
        print(error);
      }
    }
  }

// Sign out with Google
Future&amp;lt;void&amp;gt; _handleSignOut() =&amp;gt; _googleSignIn.disconnect();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Find the example wiring in the &lt;a href="https://github.com/flutter/plugins/blob/main/packages/google_sign_in/google_sign_in/example/lib/main.dart"&gt;Google sign-in example application&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  iOS additional requirement
&lt;/h4&gt;

&lt;p&gt;Note that according to &lt;a href="https://developer.apple.com/sign-in-with-apple/get-started"&gt;https://developer.apple.com/sign-in-with-apple/get-started&lt;/a&gt;, starting June 30, 2020, apps that use login services must also offer a "Sign in with Apple" option when submitting to the Apple App Store.&lt;/p&gt;

&lt;h3&gt;
  
  
  4 Result.
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XwZUzgxK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x98sz8jxh3x8jwzi6xsc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XwZUzgxK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x98sz8jxh3x8jwzi6xsc.png" alt="Result 1" width="404" height="862"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0Q3L3Uz_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9qg7pmk4oe9r0aa6odgj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0Q3L3Uz_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9qg7pmk4oe9r0aa6odgj.png" alt="Result 2" width="407" height="864"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OEXNDeRO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s1hsp4nm84vb9u3w45rj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OEXNDeRO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s1hsp4nm84vb9u3w45rj.png" alt="Result 3" width="417" height="858"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tCL2qB_N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gefk4l8dj24nuqnjfggj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tCL2qB_N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gefk4l8dj24nuqnjfggj.png" alt="Result 4" width="401" height="861"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>googlecloud</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
