I am working on a Spring-MVC application and I would like to integrate Dropbox functionality into it. As I read the examples, I saw that there is some code which I can use. But this involves the user copy pasting the access token, which is not applicable in real world applications, plus I cannot find a way to set the redirect URL when authentication is complete. What changes should I make so the code does not need to copy pasted, but can be retrieved directly.
Code :
public void connectToDropbox() {
DbxAppInfo appInfo = new DbxAppInfo(APP_KEY, APP_SECRET);
DbxRequestConfig config = new DbxRequestConfig("JavaTutorial/1.0",
Locale.getDefault().toString());
DbxWebAuthNoRedirect webAuth = new DbxWebAuthNoRedirect(config, appInfo);
// Have the user sign in and authorize your app.
String authorizeUrl = webAuth.start();
System.out.println("1. Go to: " + authorizeUrl);
System.out.println("2. Click \"Allow\" (you might have to log in first)");
// No, I dont want to copy the authorization code.
System.out.println("3. Copy the authorization code.");
String code = null;
try {
code = new BufferedReader(new InputStreamReader(System.in)).readLine().trim();
} catch (IOException e) {
e.printStackTrace();
}
Controller code :
#RequestMapping(value = "/logindropbox")
public String loginIntoDropbox(){
ConnectDropbox connectDropbox = new ConnectDropbox();
connectDropbox.connectToDropbox();
return "rediect:/dashboard";
}
There was only one answer I could find on SO, but that was of no use. Any help would be nice. Thanks a lot. :-)
[Cross-linking for reference: https://www.dropboxforum.com/hc/communities/public/questions/203308909-Dropbox-authentication-without-copy-pasting-the-access-token-manually-in-Java- ]
The Dropbox Java Core SDK tutorial does use the flow where the user copies and pastes the authorization code manually. This is done using the provided DbxWebAuthNoRedirect class.
For apps where a redirect URI can be used to deliver the authorization code automatically, you'll want to use the DbxWebAuth class instead. The documentation has some sample code:
https://dropbox.github.io/dropbox-sdk-java/api-docs/v1.7.x/com/dropbox/core/DbxWebAuth.html
There's also a "web-file-browser" sample app included with the SDK download that uses DbxWebAuth.
Related
Hello guys I run into a problem. In my application I am storing fileId's of files which user selected before in GoogleDrive file picker. Also I am storing a local copy of that files in device. After each start I want to refresh local files, so I want to download them from drive. But it is not cleare for me, how should i do this.
I saw this documentation, but I can't understand where to get driveService, which used in this code
driveService.files().get(fileId)
.executeMediaAndDownloadTo(outputStream);
I don't know what driveService is in this code. (Which class instance) and how do I get it
Help me please, thank you.
P.S.
Sorry for my bad english
you should check the documentation under resumable Media Downloads it might give you some clues.
class CustomProgressListener implements MediaHttpDownloaderProgressListener {
public void progressChanged(MediaHttpDownloader downloader) {
switch (downloader.getDownloadState()) {
case MEDIA_IN_PROGRESS:
System.out.println(downloader.getProgress());
break;
case MEDIA_COMPLETE:
System.out.println("Download is complete!");
}
}
}
OutputStream out = new FileOutputStream("/tmp/driveFile.jpg");
DriveFiles.Get request = drive.files().get(fileId);
request.getMediaHttpDownloader().setProgressListener(new CustomProgressListener());
request.executeMediaAndDownloadTo(out);
BaseClientService service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Drive API Sample",
});
You should check this stack answer, maybe it will help you understand it:
How do you create a new Google Drive Service in C# using OAuth
First thank you for reading this far.Here is a basic introduction.
I have implemented the simple authentication demo in the play 2.4 documentation.
And Here is a method where i use it.
#Security.Authenticated(Secured.class)
public Result reviewSubmit(){
//Review newReview = contactFormData.get();
//newReview.save();
Form<Review> reviewFormData = reviewForm.bindFromRequest();
User user = User.findByEmail(request().username());
if(reviewFormData.hasErrors()) {
return badRequest("Form Field has Errors" +reviewFormData);
}
Review newReview =reviewFormData.get();
newReview.user = user.fName +" " + user.lName;
newReview.save();
return redirect(routes.Reviews.review());
}
AS you can see as part of the review saving process i use
User user = User.findByEmail(request().username());
The request().username() gets the currently logged in users email address
from which i use to find the user and access there first and last names when i go to save the review object later on.
What i want to know is there a way in which i can use
if(request.username() == null ){
//Show login Button
}else{
//show Logout button
}
Inside say a main.scala.html template as an easy way of altering the main template without passing a user object from every action.
Thanks for any help or pointers provided.
N.B as i am new to play and have really struggled to understand securescoial plugins/deadbolt/play-authenticate i just want a way of obfuscating the links shown
Not sure if I am answering your question but why can't you do something like this
#if(user) {
<button>LogOut</button>
} else {
//login button
}
in your template.
Where user is passed into the template , as given in the documentation.
according to the Google Drive SDK Update sharing permissions and https://developers.google.com/drive/v2/reference/permissions I want to change the whole folder content to be available for viewing (the folder contains only images and I show them on my page with <img alt='' src='https://drive.google.com/uc?export=view&id="fileID"'/>)
so, I'm trying to use that code
PermissionList permissions = Google_DriveService.Permissions.List(fileId).Fetch();
var filePermissions = permissions.Items;
Permission permission = Google_DriveService.Permissions.Get(fileId, filePermissions[0].Id).Fetch();
permission.Role = "reader";
permission.Value = "me";
permission.Type = "anyone";
permission.WithLink = true;
var updated = Google_DriveService.Permissions.Update(permission, fileId, filePermissions[0].Id).Fetch();
but I get the error message
Google.Apis.Requests.RequestError Invalid permission type specified [400] Errors [ Message[Invalid permission type specified] Location[ - ] Reason[invalid] Domain[global] ]
what am I doing wrong ?
Tony's answer is correct and here is an addon to his instruction if anyone else is looking to test this. Note that I did not write up the code for this due to time constraints but I tested it using the Google API supplied on the Google Drive web api page. I tested this by doing the following:
Go to the following link and list all the files in your drive. https://developers.google.com/drive/v2/reference/files/list
Select one file that you want to share for read only access and copy out the "id" as per the screenshot below:
Go to the insert permission page here - Tony was correct, you do not delete the existing permission but instead, you add onto that permission: https://developers.google.com/drive/v2/reference/permissions/insert
In the API explorer, type in "role" as "reader" and "type" as "anyone".
Go to your Drive: https://drive.google.com/drive/#my-drive and you will see that the file is now shared - if you hover over the green link icon in the screenshot below, it will tell you that it is now shared.
You can also copy the link for the doc and paste it into Incognito mode in Chrome to test whether you can access the read-only version of the doc in Incognito mode and it should work.
Additional:
The code should not be too hard, here it is:
Permission newPermission = new Permission();
newPermission.setType("anyone");
newPermission.setRole("reader");
try {
return service.permissions().insert(fileId, newPermission).execute();
} catch (IOException e) {
System.out.println("An error occurred: " + e);
}
You can't change permission types with update requests, insert a new permission and delete the existing. You should also not use me as a principal related to an anyone permission. Anyone permissions dont expect value attributes to be set.
This is the new reference of the API: https://developers.google.com/drive/v3/reference/permissions/create
I use this code to publish a post on my Facebook wall from Java:
facebook.postStatusMessage("Hello World from Facebook4J.");
However, there is a problem: only I can see this post, my friends cannot.
How can it be visible to my friends?
With postStatusMessage() method, you publish a post using your default privacy setting.
Try to use postFeed() method with privacy parameter.
To post to all friends example:
PrivacyParameter privacy = new PrivacyBuilder().setValue(PrivacyType.ALL_FRIENDS).build();
PostUpdate postUpdate = new PostUpdate(new URL("http://facebook4j.org"))
.picture(new URL("http://facebook4j.org/images/hero.png"))
.name("Facebook4J - A Java library for the Facebook Graph API")
.caption("facebook4j.org")
.description("Facebook4J is a Java library for the Facebook Graph API. This library provides the ease of use like Twitter4J. Facebook4J is an unofficial library.")
.privacy(privacy);
String postId = facebook.postFeed(postUpdate);
where in is InputStream your photo
Media media = new Media("", in);
PhotoUpdate photoUpdate = new PhotoUpdate(media);
postId = facebook.postPhoto(photoUpdate);
How can I get my profile picture from my Google Talk profile using Smack API or any other API.
I tried using VCards but it does not work. I obtain only an XML representation of the users profile: FirstName LastName.
I found a solution. It seems that I cannot take the avatar picture only on the roster listener. This means that only when the presence changes you can see user's Presence.Type and can get it's avatar, but other data about his profile cannot be taken. If anyone knows why or has a solution I would be very happy to see what he has in mind.
try this it work complete.
try {
vCard.load(connection, entry.getUser());
vCard.getExtensions();
byte[] b = vCard.getAvatar();
Bitmap avatar = BitmapFactory.decodeByteArray(vCard.getAvatar(), 0, b.length);
} catch (Exception e) {
Log.e("EXP-Image", "Not valid code");
}