Reputation: 1
i think this question might be asked a Million Times but i did not find any answers out there.
I'm trying to access Google Calendar with my Unity Project, so I'm Using C# but cant use the Official google client libraries as its not supported.
Sadly Every time i try to get the Access Token from the server i get either a 404 or just a empty response.
private static string tokenUrl = "https://oauth2.googleapis.com/";
public static string GetOAuthTokens(string auth_code, string grantType)
{
RestClient rc = new RestClient(tokenUrl);
RestRequest request = new RestRequest(Method.POST);
request.Parameters.Clear();
request.Resource = "token";
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request.AddParameter("auth_code", auth_code, ParameterType.RequestBody);
request.AddParameter("client_id", client_id, ParameterType.RequestBody);
request.AddParameter("secret", client_secret, ParameterType.RequestBody);
request.AddParameter("grant_type", grantType, ParameterType.RequestBody);
request.AddParameter("redirect_uri", "http://localhost:4444", ParameterType.RequestBody);
return rc.Execute(request).Content;
}
And i've called that method with:
Debug.Log(OAuth2.GetOAuthTokens(code, "authorization_code"));
that Debug.Log
is just for debugging.
I've seen a lot of people saying i need to whitelist my Uri but i cant find that menu point in the credentials.
Im searching for about 6 hours now. Please Help!
Upvotes: 0
Views: 1681
Reputation: 1
I figured it out:
request.AddParameter("auth_code", auth_code, ParameterType.RequestBody);
had to be: request.AddParameter("code", auth_code, ParameterType.GetOrPost);
and i think i had to rename the Parameter keys to match the Documentation
Upvotes: 0
Reputation: 1430
I made a DLL file of Google Calendar using their .NET SDK as explained here. I modified it a little to work with Unity and then used that dll file in Unity project. It worked for me.
Below is DLL code:
static string[] Scopes = { CalendarService.Scope.CalendarReadonly };
static string ApplicationName = "Google Calendar API .NET Quickstart";
public void Main()
{
UserCredential credential;
using (var stream =
new FileStream(UnityEngine.Application.streamingAssetsPath + "/credentials.json", FileMode.Open, FileAccess.Read))
{
// The file token.json stores the user's access and refresh tokens, and is created
// automatically when the authorization flow completes for the first time.
string credPath = "token.json";
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
Console.WriteLine("Credential file saved to: " + credPath);
}
// Create Google Calendar API service.
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
// Define parameters of request.
EventsResource.ListRequest request = service.Events.List("primary");
request.TimeMin = DateTime.Now;
request.ShowDeleted = false;
request.SingleEvents = true;
request.MaxResults = 10;
request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;
// List events.
Events events = request.Execute();
Debug.Log("Upcoming events:");
if (events.Items != null && events.Items.Count > 0)
{
foreach (var eventItem in events.Items)
{
string when = eventItem.Start.DateTime.ToString();
if (String.IsNullOrEmpty(when))
{
when = eventItem.Start.Date;
}
Debug.Log("Event Summery: " + eventItem.Summary + " | " + " On " + when);
}
}
else
{
Debug.Log("No upcoming events found.");
}
Console.Read();
}
On Unity Side Code:
public Calendar calendar = new Calendar();
private void Start()
{
calendar.Main();
}
It gives me calendar event summery and date. You can download project from here.
Upvotes: 2