Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when using with multiple sharepoint sites #31

Closed
homebrew79 opened this issue Aug 18, 2020 · 7 comments
Closed

Error when using with multiple sharepoint sites #31

homebrew79 opened this issue Aug 18, 2020 · 7 comments
Assignees
Labels
bug Something isn't working

Comments

@homebrew79
Copy link

homebrew79 commented Aug 18, 2020

Describe the bug
Trying to use addin auth strategy across multiple sharepoint online sites. Specifically using chunked file upload.
The file uploads fine to the first site, but when uploading to the second site, I receive the following error:

unable to request api: invalid character '<' looking for beginning of value

Versions
Sharepoint Online
github.com/koltyakov/gosip v0.0.0-20200628141644-21f34db9ce21

To Reproduce

package main

import (
	"log"
	"os"

	"github.com/koltyakov/gosip"
	"github.com/koltyakov/gosip/api"
	strategy "github.com/koltyakov/gosip/auth/addin"
)

type site struct {
	url             string
	clientID        string
	secret          string
	uploadDirectory string
}

func main() {
	demoSite := site{
		url:             "https://example.sharepoint.com/sites/MyExampleSite",
		clientID:        "myclientid",
		secret:          "mysecret",
		uploadDirectory: "Shared Documents",
	}

	secondSite := site{
		url:             "https://example.sharepoint.com/sites/MyExampleSite2",
		clientID:        "myotherclientid",
		secret:          "myothersecret",
		uploadDirectory: "Shared Documents",
	}

	err := uploadFile(demoSite)
	if err != nil {
		log.Fatal(err)
	}

	err = uploadFile(secondSite)
	if err != nil {
		log.Fatal(err)
	}
}

func uploadFile(s site) error {
	authCnfg := strategy.AuthCnfg{
		SiteURL:      s.url,
		ClientID:     s.clientID,
		ClientSecret: s.secret,
	}
	client := gosip.SPClient{AuthCnfg: &authCnfg}
	apiClient := api.NewSP(&client)
	folder := apiClient.Web().GetFolder(s.uploadDirectory)

	file, err := os.Open("testfile.pdf")
	if err != nil {
		log.Fatalf("unable to read a file: %v\n", err)
	}
	defer file.Close()

	_, err = folder.Files().AddChunked("MyTestFile.pdf", file, nil)
	if err != nil {
		return (err)
	}

	return nil
}

Expected behavior
Expect for file to upload to both sites without error.

@homebrew79 homebrew79 added the bug Something isn't working label Aug 18, 2020
@koltyakov koltyakov self-assigned this Aug 18, 2020
@koltyakov koltyakov added the investigate Research what's going on label Aug 18, 2020
@koltyakov
Copy link
Owner

Thanks for reporting an issue. I'll investigate what it might be.
I believe the multi-context scenario is covered. However, it might be AddIn-Only Auth strategy specific.

@homebrew79
Copy link
Author

After running it through delve, it appears the issue might be with the getRealm function. In my case, both of the sites I want to access reside at the same host URL with different paths. So it is reusing the cacheKey. If I comment out the caching bits, everything runs fine.

@koltyakov
Copy link
Owner

koltyakov commented Aug 18, 2020

That's strange as not only host but also strategy and part of secrets act as a cache key:

cacheKey := parsedURL.Host + "@addinonly@" + c.ClientID + "@" + c.ClientSecret
if accessToken, found := storage.Get(cacheKey); found {
  return accessToken.(string), nil
}

UPD: Oh, got it, getRealm, yep this might be the root issue.

@homebrew79
Copy link
Author

Thanks for looking into this. Here is what I'm referencing:

cacheKey := parsedURL.Host + "@realm"

koltyakov added a commit that referenced this issue Aug 18, 2020
@koltyakov
Copy link
Owner

@homebrew79, thank you so much for assisting finding the issue, I appreciate it!
Pushed the update which makes that cache key unique within the same environment and using 2 or more AddIns registrations. Could you please check is it helps you?

@koltyakov koltyakov removed the investigate Research what's going on label Aug 18, 2020
@homebrew79
Copy link
Author

That seems to have fixed it. I will do some more extensive testing, but I think this can be closed. Thanks for the quick response!

@koltyakov
Copy link
Owner

Thanks for using the package!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants