ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 450|ตอบกลับ: 0

[VB.NET] การทำ Login เข้าสู่ระบบของ Google Sheets

[คัดลอกลิงก์]

311

กระทู้

502

โพสต์

6060

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
6060




Add References ...



Youtube แสดงวิธีการขั้นตอนในการรับไฟล์ Credentials ... หรือ Google API ซึ่งจะอยู่ในรูปแบบของ JSON


ํYoutube แสดงวิธีการกำหนดสิทธิการเข้าถึง Google Sheets ...

ตัวอย่าง Google Sheets ซึ่งตอนนี้แอดมินได้เปิดให้เป็นแบบ Editor เพื่อให้ได้ลองทำการเขียนอ่านข้อมูลได้ ...


พื้นฐานของการใช้งาน List (Of Object)
...


มาดูโค้ดฉบับเต็มกันเถอะ ...
  1. Imports Google.Apis.Sheets.v4
  2. Imports Google.Apis.Sheets.v4.Data

  3. Public Class frmLogin

  4.     Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
  5.         If Trim(txtUsername.Text.Trim.Length) = 0 Then
  6.             MessageBox.Show("Enter your Username.", "Report status", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  7.             txtUsername.Focus()
  8.             Return
  9.         ElseIf Trim(txtPassword.Text.trim.Length) = 0 Then
  10.             MessageBox.Show("Enter your Password.", "Report status", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  11.             txtPassword.Focus()
  12.             Return
  13.         End If
  14.         '// If Login Successful then Close Form Login.
  15.         If LoginSystem() Then Me.Close()
  16.     End Sub

  17.     ' / --------------------------------------------------------------------------------
  18.     Private Function LoginSystem() As Boolean
  19.         LoginSystem = False
  20.         '// Define Sheet Name.
  21.         Dim SheetName As String = "UserData"
  22.         '// Range A1:B1 is Column Header.
  23.         Dim range As String = SheetName + "!A2:B100"    '// Select range column A is UserName and column B is Password.
  24.         '// Initialize Google Sheets API.
  25.         Call Credentials()
  26.         '// Read data from the specified range.
  27.         Dim request As SpreadsheetsResource.ValuesResource.GetRequest = Service.Spreadsheets.Values.Get(SpreadsheetId, range)
  28.         Dim response As ValueRange = request.Execute()
  29.         '// Process the data.
  30.         If response IsNot Nothing AndAlso response.Values IsNot Nothing Then
  31.             For Each row As List(Of Object) In response.Values
  32.                 If row(0).ToString() = txtUsername.Text.Trim AndAlso row(1).ToString() = txtPassword.Text.Trim Then
  33.                     '// Login Successful.
  34.                     MessageBox.Show("Login successful!", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Information)
  35.                     Return True
  36.                 End If
  37.             Next
  38.             '// Login failed.
  39.             MessageBox.Show("Invalid username or password.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  40.             'Return False
  41.         Else
  42.             MessageBox.Show("No data found.", "Report Status", MessageBoxButtons.OK, MessageBoxIcon.Warning)
  43.         End If
  44.         '//
  45.     End Function

  46.     Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
  47.         Me.Close()
  48.         Me.Dispose()
  49.         Application.Exit()
  50.     End Sub

  51.     Private Sub frmLogin_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  52.         Me.Dispose()
  53.         GC.SuppressFinalize(Me)
  54.     End Sub

  55.     Private Sub frmLogin_Load(sender As Object, e As System.EventArgs) Handles Me.Load
  56.         '//
  57.     End Sub

  58.     Private Sub txtUsername_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtUsername.KeyPress
  59.         If Asc(e.KeyChar) = 13 Then
  60.             e.Handled = True
  61.             SendKeys.Send("{TAB}")
  62.         End If
  63.     End Sub

  64.     Private Sub txtPassword_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtPassword.KeyPress
  65.         If Asc(e.KeyChar) = 13 Then
  66.             e.Handled = True
  67.             SendKeys.Send("{TAB}")
  68.         End If
  69.     End Sub
  70. End Class
คัดลอกไปที่คลิปบอร์ด


โมดูลในการกำหนดค่าที่สำคัญของ Google Sheets เช่น SpreadSheetID, Application Name และไฟล์ Credentials
  1. Imports Google.Apis.Auth.OAuth2
  2. Imports Google.Apis.Services
  3. Imports Google.Apis.Sheets.v4

  4. Module modCredentials
  5.     Public Service As SheetsService
  6.     Public SpreadsheetId As String = "1nBOWl-PDGwYng6IOifi6bhoLfvamTn-45CWtn5t59qs"
  7.     Public ApplicationName = "SampleSheet" '// Same as Filename.
  8.     '// JSON credential file path.
  9.     Public CredentialFilePath As String = MyPath(Application.StartupPath) & "credentials\GoogleSheet.json"

  10.     ' / ------------------------------------------------------------------------------------------------
  11.     ' / Initialize Google Sheets API.
  12.     ' / ------------------------------------------------------------------------------------------------
  13.     Public Sub Credentials()
  14.         '// Load credentials from JSON file
  15.         Dim credential = GoogleCredential.FromFile(CredentialFilePath).CreateScoped(SheetsService.Scope.Spreadsheets)
  16.         '// Create Google Sheets API service
  17.         Service = New SheetsService(New BaseClientService.Initializer() With {
  18.             .HttpClientInitializer = credential,
  19.             .ApplicationName = ApplicationName
  20.         })
  21.     End Sub
  22. End Module
คัดลอกไปที่คลิปบอร์ด


ดาวน์โหลดโค้ดฉบับเต็ม VB.NET (2017) ได้จากที่นี่ ...

ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-4-30 02:26 , Processed in 0.502563 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้