public static void Initialize(TestContext context) { UserAccessToken = context.Properties["userAccessToken"].ToString(); Client = new DropboxClient(UserAccessToken); var teamToken = context.Properties["teamAccessToken"].ToString(); TeamClient = new DropboxTeamClient(teamToken); var appKey = context.Properties["appKey"].ToString(); var appSecret = context.Properties["appSecret"].ToString(); AppClient = new DropboxAppClient(appKey, appSecret); }
/// <summary> /// Determines the type of dropbox oAuth token we have. Optionally upgrades to an oAuth 2.0 credential and/or disables the existing one. /// MODIFIES THE IN MEMORY USER PROFILE to use an upgraded credential /// </summary> /// <param name="pf">The user profile</param> /// <param name="fCommit">True to update the database with the oAuth 2.0 credential</param> /// <param name="fDisable">True to disable the old oAuth 1.0</param> /// <returns>The state of the dropbox access token PRIOR to upgrade.</returns> async public Task <TokenStatus> ValidateDropboxToken(MyFlightbook.Profile pf, bool fCommit = false, bool fDisable = false) { TokenStatus result = TokenStatus.None; if (pf == null || String.IsNullOrEmpty(pf.DropboxAccessToken)) { return(result); } try { string dbAppKey = AppKey; string dbSecret = AppSecret; byte[] rgbOAuth1Token = Convert.FromBase64String(pf.DropboxAccessToken); string xmlOAuth1Token = System.Text.Encoding.Default.GetString(rgbOAuth1Token); // if we get here, it is probably an oAuth1 token if (xmlOAuth1Token.Trim().StartsWith("<", StringComparison.OrdinalIgnoreCase)) { string szRawToken = null; string szRawSecret = null; using (MemoryStream stream = new MemoryStream(rgbOAuth1Token)) { System.Runtime.Serialization.DataContractSerializer serializer = new System.Runtime.Serialization.DataContractSerializer(typeof(Dictionary <string, string>)); Object o = serializer.ReadObject(stream); if (o.GetType().Equals(typeof(Dictionary <string, string>))) { Dictionary <string, string> d = (Dictionary <string, string>)o; szRawToken = d["TokenDropBoxUsername"]; szRawSecret = d["TokenDropBoxPassword"]; } } try { DropboxAppClient client = new DropboxAppClient(dbAppKey, dbSecret); var tokenFromOAuth1Result = await client.Auth.TokenFromOauth1Async(szRawToken, szRawSecret); pf.DropboxAccessToken = tokenFromOAuth1Result.Oauth2Token; if (fCommit) { pf.FCommit(); } result = TokenStatus.oAuth1; } catch (WebException ex) { Stream ResponseStream = ex.Response.GetResponseStream(); StreamReader reader = new System.IO.StreamReader(ResponseStream, System.Text.Encoding.Default); string szResult = reader.ReadToEnd(); } } else { result = TokenStatus.oAuth2; } } catch (FormatException) { // It should be v2! result = TokenStatus.oAuth2; } return(result); }