KoenZomers / OneDriveAPI

API in .NET Framework 4.8.0, .NET Core 3.1 and .NET 6.0 to communicate with OneDrive Personal and OneDrive for Business
Eclipse Public License 1.0
110 stars 34 forks source link

System.NullReferenceException:“Object reference not set to an instance of an object.” #26

Closed bbhxwl closed 3 years ago

bbhxwl commented 4 years ago

System.NullReferenceException:“Object reference not set to an instance of an object.”

var oneDrive = new OneDriveForBusinessO365Api("****", "****"); var list=await oneDrive.GetDriveRootChildren();

I have office 365 home edition, why does this code make mistakes?

KoenZomers commented 3 years ago

I'm having a hard time following what you're trying to achieve. Few pointers from my side which hopefully help you forward:

  1. Use the OneDriveGraphApi instead of the OneDriveForBusinessO365Api as the latter is deprecated
  2. The way oAuth works, depending on the auth flow you're using, is that you get an authorization code. You can redeem that code for an access token using i.e. protected abstract Task<OneDriveAccessToken> GetAccessTokenFromAuthorizationToken(string authorizationToken);. Depending on the scopes you have requested, it may or may not return a refresh token: public string RefreshToken { get; set; }. You can use this refresh token to request a new access token after your application closes: protected abstract Task<OneDriveAccessToken> GetAccessTokenFromRefreshToken(string refreshToken);. You need to securely store the refresh token yourself. And ensure each time you retrieve a new access token with it, you take the new refresh token and update your stored value.
  3. You may consider using MSAL (https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-overview) which abstracts the process of working with and storing these tokens from you. However, my API does not work with MSAL, so you will have to make raw HTTP REST requests if you go for that option.
bbhxwl commented 3 years ago

I'm having a hard time following what you're trying to achieve. Few pointers from my side which hopefully help you forward:

  1. Use the OneDriveGraphApi instead of the OneDriveForBusinessO365Api as the latter is deprecated
  2. The way oAuth works, depending on the auth flow you're using, is that you get an authorization code. You can redeem that code for an access token using i.e. protected abstract Task<OneDriveAccessToken> GetAccessTokenFromAuthorizationToken(string authorizationToken);. Depending on the scopes you have requested, it may or may not return a refresh token: public string RefreshToken { get; set; }. You can use this refresh token to request a new access token after your application closes: protected abstract Task<OneDriveAccessToken> GetAccessTokenFromRefreshToken(string refreshToken);. You need to securely store the refresh token yourself. And ensure each time you retrieve a new access token with it, you take the new refresh token and update your stored value.
  3. You may consider using MSAL (https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-overview) which abstracts the process of working with and storing these tokens from you. However, my API does not work with MSAL, so you will have to make raw HTTP REST requests if you go for that option.

I want to do a similar function, code can be obtained once, do not need to get the program in the future? Because of oauth2.0, at present, I can only get things manually or use browser.

https://github.com/WangNingkai/OLAINDEX

bbhxwl commented 3 years ago

I mainly want to back up MySQL database automatically on my server, and then upload it to onedrive automatically with my own program. Implementation of unmanned management procedures.

KoenZomers commented 3 years ago

Wouldn't it be much easier to just write a PowerShell script to create the backup and use PnP PowerShell to upload the backup to your OneDrive afterwards? Writing an entire application for just his feels like a bit of overkill.

bbhxwl commented 3 years ago

Wouldn't it be much easier to just write a PowerShell script to create the backup and use PnP PowerShell to upload the backup to your OneDrive afterwards? Writing an entire application for just his feels like a bit of overkill.

My programs are all net core 3.1, running on Linux system. And if you can upload your own program, there will be many possibilities.

bbhxwl commented 3 years ago

I'm having a hard time following what you're trying to achieve. Few pointers from my side which hopefully help you forward:

  1. Use the OneDriveGraphApi instead of the OneDriveForBusinessO365Api as the latter is deprecated
  2. The way oAuth works, depending on the auth flow you're using, is that you get an authorization code. You can redeem that code for an access token using i.e. protected abstract Task<OneDriveAccessToken> GetAccessTokenFromAuthorizationToken(string authorizationToken);. Depending on the scopes you have requested, it may or may not return a refresh token: public string RefreshToken { get; set; }. You can use this refresh token to request a new access token after your application closes: protected abstract Task<OneDriveAccessToken> GetAccessTokenFromRefreshToken(string refreshToken);. You need to securely store the refresh token yourself. And ensure each time you retrieve a new access token with it, you take the new refresh token and update your stored value.
  3. You may consider using MSAL (https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-overview) which abstracts the process of working with and storing these tokens from you. However, my API does not work with MSAL, so you will have to make raw HTTP REST requests if you go for that option. I'm using this one. Shouldn't I use OneDriveConsumerApi?

image

bbhxwl commented 3 years ago

Office 365 home and personal versions, can I use this class?

OneDriveForBusinessO365Api

bbhxwl commented 3 years ago

I'm having a hard time following what you're trying to achieve. Few pointers from my side which hopefully help you forward:

  1. Use the OneDriveGraphApi instead of the OneDriveForBusinessO365Api as the latter is deprecated
  2. The way oAuth works, depending on the auth flow you're using, is that you get an authorization code. You can redeem that code for an access token using i.e. protected abstract Task<OneDriveAccessToken> GetAccessTokenFromAuthorizationToken(string authorizationToken);. Depending on the scopes you have requested, it may or may not return a refresh token: public string RefreshToken { get; set; }. You can use this refresh token to request a new access token after your application closes: protected abstract Task<OneDriveAccessToken> GetAccessTokenFromRefreshToken(string refreshToken);. You need to securely store the refresh token yourself. And ensure each time you retrieve a new access token with it, you take the new refresh token and update your stored value.
  3. You may consider using MSAL (https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-overview) which abstracts the process of working with and storing these tokens from you. However, my API does not work with MSAL, so you will have to make raw HTTP REST requests if you go for that option.

new OneDriveGraphApi does not GetAccessTokenFromAuthorizationToken and GetAccessTokenFromRefreshToken

bbhxwl commented 3 years ago

I'm having a hard time following what you're trying to achieve. Few pointers from my side which hopefully help you forward:

  1. Use the OneDriveGraphApi instead of the OneDriveForBusinessO365Api as the latter is deprecated
  2. The way oAuth works, depending on the auth flow you're using, is that you get an authorization code. You can redeem that code for an access token using i.e. protected abstract Task<OneDriveAccessToken> GetAccessTokenFromAuthorizationToken(string authorizationToken);. Depending on the scopes you have requested, it may or may not return a refresh token: public string RefreshToken { get; set; }. You can use this refresh token to request a new access token after your application closes: protected abstract Task<OneDriveAccessToken> GetAccessTokenFromRefreshToken(string refreshToken);. You need to securely store the refresh token yourself. And ensure each time you retrieve a new access token with it, you take the new refresh token and update your stored value.
  3. You may consider using MSAL (https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-overview) which abstracts the process of working with and storing these tokens from you. However, my API does not work with MSAL, so you will have to make raw HTTP REST requests if you go for that option.

My English is too stupid, I seem to understand now, the first time to get OneDriveApi.AccessToken.RefreshToken Save it, await OneDriveApi.AuthenticateUsingRefreshToken ( RefreshTokenTextBox.Text ); Reload, right? After the next load, OneDriveApi.AccessToken.RefreshToken Will it change? Or is this value fixed in the future and will not be changed?

bbhxwl commented 3 years ago

It seems that OneDriveGraphApi can only be used in the commercial version, and can not be used by office individuals. OneDriveConsumerApi API can be used for personal use? The OneDriveGraphApi of demo does not work properly。I made a lot of manual changes to make it work.

KoenZomers commented 3 years ago

Office 365 home and personal versions, can I use this class?

OneDriveForBusinessO365Api

No you cannot. It was meant to be used with OneDrive for Business only and also for that purpose it has been deprecated. You should use the OneDriveGraphApi regardless whether you're connecting to a OneDrive Consumer account or a OneDrive for Business account.

KoenZomers commented 3 years ago

I'm having a hard time following what you're trying to achieve. Few pointers from my side which hopefully help you forward:

  1. Use the OneDriveGraphApi instead of the OneDriveForBusinessO365Api as the latter is deprecated
  2. The way oAuth works, depending on the auth flow you're using, is that you get an authorization code. You can redeem that code for an access token using i.e. protected abstract Task<OneDriveAccessToken> GetAccessTokenFromAuthorizationToken(string authorizationToken);. Depending on the scopes you have requested, it may or may not return a refresh token: public string RefreshToken { get; set; }. You can use this refresh token to request a new access token after your application closes: protected abstract Task<OneDriveAccessToken> GetAccessTokenFromRefreshToken(string refreshToken);. You need to securely store the refresh token yourself. And ensure each time you retrieve a new access token with it, you take the new refresh token and update your stored value.
  3. You may consider using MSAL (https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-overview) which abstracts the process of working with and storing these tokens from you. However, my API does not work with MSAL, so you will have to make raw HTTP REST requests if you go for that option.

My English is too stupid, I seem to understand now, the first time to get OneDriveApi.AccessToken.RefreshToken Save it, await OneDriveApi.AuthenticateUsingRefreshToken ( RefreshTokenTextBox.Text ); Reload, right? After the next load, OneDriveApi.AccessToken.RefreshToken Will it change? Or is this value fixed in the future and will not be changed?

Sounds about right. The RefreshToken is valid for 90 days from the moment you received it. You can use it for subsequent authentication calls within those 90 days. If you authenticate using the refresh token, be sure to take the updated refresh token on the authenticated connection and store that somewhere securely. If you do that and ensure you at least once every 90 days get a new access token from the refresh token and keep the refresh token in the response, you can endlessly keep using it.

KoenZomers commented 3 years ago

It seems that OneDriveGraphApi can only be used in the commercial version, and can not be used by office individuals. OneDriveConsumerApi API can be used for personal use? The OneDriveGraphApi of demo does not work properly。I made a lot of manual changes to make it work.

The Graph API should work for both consumer and business accounts. If for some reason it doesn't work well against consumer accounts, it is still a supported option to use the OneDriveConsumerApi instead.

KoenZomers commented 3 years ago

Closing issue as no further information has been received. Feel free to reopen in case the issue has not been resolved yet.