私はGoogle.Apis.Calendar.v3
を使用して解決策を見つけました。私はここに投稿していますので、他の人を助けるかもしれません。
第1のリフレッシュトークンを使用して新しいアクセストークンを取得:
string postString = "client_id=yourclientid";
postString += "&client_secret=youclientsecret&refresh_token=userrefreshtoken";
postString += "&grant_type=refresh_token";
string url = "https://www.googleapis.com/oauth2/v4/token";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url.ToString());
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
UTF8Encoding utfenc = new UTF8Encoding();
byte[] bytes = utfenc.GetBytes(postString);
Stream os = null;
request.ContentLength = bytes.Length;
os = request.GetRequestStream();
os.Write(bytes, 0, bytes.Length);
GoogleToken token = new GoogleToken();
HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();
Stream responseStream = webResponse.GetResponseStream();
StreamReader responseStreamReader = new StreamReader(responseStream);
string result = responseStreamReader.ReadToEnd();
JavaScriptSerializer serializer = new JavaScriptSerializer();
token = serializer.Deserialize<GoogleToken>(result);
その後、資格証明書を作成するためにTOKEを使用し、トークンリフレッシュ次は、ユーザーのリフレッシュトークンを持っている場合、イベントのリストを取得するためのコードです。
var flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = new ClientSecrets
{
ClientId = yourclientid,
ClientSecret = yourclientsecret
},
Scopes = new[] { CalendarService.Scope.Calendar }
});
var credential = new UserCredential(flow, Environment.UserName, new TokenResponse
{
AccessToken = token.access_token,
RefreshToken = userrefreshtoke
});
CalendarService service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "application name",
});
var list = service.CalendarList.List().Execute().Items;
foreach (var c in list)
{
var events = service.Events.List(c.Id).Execute().Items.Where(i => i.Start.DateTime >= DateTime.Now).ToList();
foreach (var e in events)
{
}
}
GoogleTokenクラス:あなたは返信用
public class GoogleToken
{
public string access_token { get; set; }
public string token_type { get; set; }
public string expires_in { get; set; }
}
感謝。私のシナリオは異なります。私は他のユーザーに私のGoogleアプリケーションを通して認証するカレンダーイベントを取得したい。ユーザーイベントへのオフラインアクセス。 –