Android Question oauth2 and GMail Rest Api

mike1967

Active Member
Licensed User
Longtime User
Hello usig this sample code i can send email from user email using GMail Rest Api, it work fine :
Code:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private oauth2 As GoogleOAuth2
    Private ClientId As String = "<apicode>.apps.googleusercontent.com"
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        oauth2.Initialize(Me, "oauth2", ClientId, "https://www.googleapis.com/auth/gmail.send")
    End If
    Activity.LoadLayout("1")
End Sub

Sub Activity_Resume
    oauth2.CallFromResume(Activity.GetStartingIntent)
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    Dim msg As MailCreator
    msg.Initialize
    msg.HtmlBody = True
    msg.ToList.Add("email address")
    msg.Subject = "test"
    msg.Body = $"First line<br/>second line"$
    'add attachment
    Dim fd As MultipartFileData
    fd.ContentType = "image/jpeg"
    fd.Dir = File.DirAssets
    fd.FileName = "android.png"
    msg.Attachments.Add(fd)
    Send(msg)
End Sub

Sub Send (msg As MailCreator)
    oauth2.GetAccessToken
    Wait For OAuth2_AccessTokenAvailable (Success As Boolean, Token As String)
    If Success = False Then
        Log("Error accessing account.")
        Return
    End If
    Dim j As HttpJob
    j.Initialize("", Me)
    j.PostString("https://www.googleapis.com/upload/gmail/v1/users/me/messages/send?uploadType=multipart", msg.ToString)
    j.GetRequest.SetHeader("Authorization", "Bearer " & Token)
    j.GetRequest.SetContentType("message/rfc822")
    Wait For (j) JobDone(j As HttpJob)
    If j.Success Then
        Log("Mail sent successfully")
        Log(j.GetString)
        ToastMessageShow("Message sent", True)
    Else
        Log("Failed to send mail.")
    End If
    j.Release
    Log("Done!!!")
End Sub

Now , some one can help me in oder to modify this code to get and show also the user display name necessable for my app ?
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…