Search code examples
c#.netunity-game-enginegmailgmail-api

GMail API Error: Path "{userId}/messages/send" misses a "userId" parameter. Is this a bug?


I am attempting to send email via the .NET API for GMail from within a Unity3D script. Unfortunately I'm having some trouble. The following code is called whenever I press the "R" key in-game.

//MEMBERS INCLUDED AT TOP OF CLASS
//TextAsset credFile;
//static String AppName = "Redacted-Appname";
//
//List<string> Scopes = new List<string>() { "GMAIL_SEND" };



if (Input.GetKeyDown(KeyCode.R))
    {
        UserCredential emailerCredential;

        //Create Credential
        using (MemoryStream credStream = new MemoryStream(credFile.bytes))
        {
            string credPath = "token.json";
            emailerCredential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(credStream).Secrets,
                Scopes,
                "[email protected]",
                CancellationToken.None,
                new FileDataStore(credPath, true)).Result;
        }

        //Create Service using credential
        GmailService service = new GmailService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = emailerCredential,
            ApplicationName = AppName
        });

        //Create Mail Message
        MailMessage message = new MailMessage(
            "[email protected]",
            "[email protected]",
            "Test Email Using GMail API",
            "This is a test of the emergency llama system.");

        //Create Gmail Message
        Message emailmsg = new Message();
        //ConvertMail Message to URL Encoded Base64 string and feed it into Gmail message
        emailmsg.Raw = HttpUtility.UrlEncode(Convert.ToBase64String(Encoding.ASCII.GetBytes(message.ToString())));
        string userId = "[email protected]";
        service.Users.Messages.Send(emailmsg, userId).Execute();
    }

As a result of this code, I end up with the following exception:

ArgumentException: Path "{userId}/messages/send" misses a "userId" parameter UnityLoader.js:1043 Parameter name: {userId}/messages/send UnityLoader.js:1043 at Google.Apis.Requests.RequestBuilder.BuildRestPath () [0x00000] in <00000000000000000000000000000000>:0 UnityLoader.js:1043 --- End of stack trace from previous location where exception was thrown --- UnityLoader.js:1043 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x00000] in <00000000000000000000000000000000>:0

I believe that the Request builder is building {userId} into the REST request instead of inserting the actual username. Looking for someone with more experience to confirm/deny this. Is this a bug, or am I doing it wrong?

Assuming that this is a bug I'm going to go post it on the GMail.Net API github here: https://github.com/googleapis/google-api-dotnet-client/issues


Solution

  • I posted this as a bug report to the Google Github and it was recorded there as such. I was, however, informed that Unity3D was not a supported platform, and as such the issue was of low priority. If anyone wants to fix this, they'll have to branch the repo.